Cannot render boolean value in JSX?

前端 未结 3 1279
予麋鹿
予麋鹿 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(
      
    Boolean Value: {ipsumText + ''}
    , document.getElementById('impl') );

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

    var ipsumText = true + '';
    
    ReactDOM.render(
      
    Boolean Value: {ipsumText}
    , 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';
    

提交回复
热议问题