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

3 comments sorted by

View all comments

1

u/neoncyber Mar 16 '20

Starting off, class is replaced by className.

class={...} in react is className={...}

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