122

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 workaround for this?

Here is an example

var ipsumText = true;

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

Just shows compiled HTML as

<div data-reactid=".0"><span data-reactid=".0.0">Boolean Value:    </span></div>

EDIT: Here is the JSBin link for the example http://jsbin.com/nibihodoce/1/edit?html,js,output

EDIT 2: I have already explored the .toString() alternative, however since I am iterating over an array of objects and a particular field of that object can have string/integer/boolean kind of value. Applying .toString() to all of 'em doesn't seem optimal.

1
  • 1
    "Applying .toString() to all of 'em doesn't seem optimal" -- Use { String( value ) } then. Nothing is more optimal and universal than that. Commented Jul 13, 2016 at 0:12

4 Answers 4

215
Boolean Value: { ipsumText.toString() }

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 ).

Sign up to request clarification or add additional context in comments.

3 Comments

Why do we need do type cast? why can't boolean show directly?
React doesn't allow object as values, But Vue does show the actual values apart from undefined and null. I belilve it's using toString underhood but why react won't do that?
@Naren Angular works the same as in Vue in this regard, which I think is the desirable default behavior.
3

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';

2 Comments

In case the value isn't boolean, I would wish to show it as is.
In this case, use this condition: var ipsumText = typeof value === 'boolean' ? value + '' : value; We are checking if variable is boolean, when we are converting it to string, else displaying it as is.
2

@gaperton's answer is fantastic. I have another solution. here it is:

{ipsumText?'true':'false'}

1 Comment

I think because of how the problem got edited it lost some context. ipsumText isn't supposed to be boolean all the time, it could be string as well. We don't know it upfront what data type we shall be getting.
-2
<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>

1 Comment

Please explain your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.