Ternary style operator with React Js Es 6

I am trying to add the following ternary operator to show my button if I am logged in and if I will not hide it. The following holds me in error.

<img src={this.state.photo} alt="" style="{isLoggedIn ? 'display:' : 'display:none'}" /> 
+9
source share
2 answers

What you provide to the style attribute must be an object. Since we are writing js code in jsx between braces, you will insert an object there.

Remember that you need to camel all css details. (font-size ==> fontSize)

 <img src={this.state.photo} alt="" style={ isLoggedIn ? { display:'block'} : {display : 'none'} } /> 

or

 <img src={this.state.photo} alt="" style={ { display: isLoggedIn ? 'block' : 'none' } } /> 
+24
source

The three should be as below:

 style={isLoggedIn ? display:'block' : display:'none'} 

Remove the quotation marks - then this should work (assuming that isLoggedIn is a logical isLoggedIn ).

-1
source

Source: https://habr.com/ru/post/1261115/


All Articles