Cannot render boolean value in JSX?

前端 未结 3 1268
予麋鹿
予麋鹿 2020-12-08 12:39

I am trying to render boolean value inside JSX, however React is evaluating it as expression and isn\'t returning anything after the component is returned.

Any worka

相关标签:
3条回答
  • 2020-12-08 13:22

    You can convert boolean value to string, concatenating it with empty string:

    var ipsumText = true;
    
    ReactDOM.render(
      <div>
         Boolean Value: {ipsumText + ''}
      </div>,
      document.getElementById('impl')
    );
    

    Or you can do it, when assigning bool value to variable:

    var ipsumText = true + '';
    
    ReactDOM.render(
      <div>
         Boolean Value: {ipsumText}
      </div>,
      document.getElementById('impl')
    );
    

    If your variable can have not boolean value, you should convert it to boolean:

    // `ipsumText` variable is `true` now.
    var ipsumText = !!'text';
    
    0 讨论(0)
  • 2020-12-08 13:33
    Boolean Value: { ipsumText.toString() }
    

    UPD: or

    Boolean Value: { String(ipsumText) }
    

    or

    Boolean Value: { '' + ipsumText }
    

    or

    {`Boolean Value: ${ipsumText}`}
    

    or

    Boolean Value: { JSON.stringify(ipsumText) }
    

    I prefer the second option. Universal, fast, works for all primitive types: Boolean( smth ), Number( smth ).

    0 讨论(0)
  • 2020-12-08 13:45
                 <select>
                     <option value={row.feature_product ? true: true || row.feature_product ? false: false}>
                       {`${row.feature_product}`}
                     </option>
                      <option value={row.feature_product ? false: true || row.feature_product ? true: false}>
                        {`${row.feature_product ? false: true}` || `${row.feature_product ? true: false}`}
                        </option>
                    </select>
    
    0 讨论(0)
提交回复
热议问题