Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot render boolean value in JSX?

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.

like image 354
anuj_io Avatar asked Jul 12 '16 19:07

anuj_io


People also ask

How do you display boolean in JSX?

Use the String() function to render a boolean value in JSX in React, e.g. <h2>{String(bool1)}</h2> . By default boolean values don't render anything in React, so we have to convert the value to a string in order to render it. Copied! We have to convert boolean values to a string in order to show them in our JSX code.

How do you pass a Boolean value in React?

To pass a boolean as props to a component in React, wrap the boolean in curly braces, e.g. <Child bool={true} /> . All props you pass to a component that are not of type string have to be wrapped in curly braces.

How do you define a boolean variable in React native?

So you can just create a boolean the way you would with any of those. For instance: var myBoolean = true; You can create the boolean in pretty much any part of your code: inside a function, as a global variable, as an object property...

How do you write if condition in JSX?

We can embed any JavaScript expression in JSX by wrapping it in curly braces. But only expressions not statements, means directly we can not put any statement (if-else/switch/for) inside JSX.


Video Answer


2 Answers

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

like image 110
gaperton Avatar answered Oct 05 '22 21:10

gaperton


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'; 
like image 24
1ven Avatar answered Oct 05 '22 23:10

1ven