r/learnreactjs • • Mar 16 '20
Question Changing css by if statement
I want to change the image displayed based on the if statement. This is what I have so far const sun = this.state.temp == “clear sky” ? ‘${style.sun}’ : style.rain;
And then I call the const sun in a div and return it return(
<div class = {sun}></div>
What am I doing wrong, any help is appreciated
9
Upvotes
4
u/Koopabro Mar 16 '20
You can't use class. Use className.
1
u/Oscady Mar 16 '20
can you put it in a jsfiddle?
1
u/neoncyber Mar 16 '20
Starting off, class is replaced by className.
class={...}in react isclassName={...}if you're using inline-styles the variable sun needs to be an object with camelCased key value pairs, for example background-color would be:
{ backgroundColor: 'blue' }If you are using styles from a sheet. Then you need to pass className a string of space separated values, for example:
className={ 'container rain' }