0

I have an element with a list of classes e.g.

<div class="class1 class2 class3">Content</div>

And I want to add this react variable to the list of classes:

{this.state.active ? ' is-active': null}

How would I go about adding it into the list, something like this?

<div class={{this.state.active ? ' is-active': null} + " class1 class2 class3"}>Content</div>

I'm having no luck at all... I'm also kinda new to react :/

2 Answers 2

5

you can do the following using template strings:

<div className={`${this.state.active ? 'is-active': ''} class1 class2 class3`}>Content</div>
Sign up to request clarification or add additional context in comments.

Comments

0

this will work but it's not very good coding

you can do this like this

<div class={{this.state.active ? " class1 class2 class3 is-active": "class1 class2 class3"}}>Content</div>

2 Comments

Is there anyway that is better practice then?
yeah the answer down is better coding

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.