1

I know this has been answered but I couldn't figure out what is wrong with my code here. I'm trying to add a class based of the conditional field.

React Code:

<div className="boards {task.IsClosed !== 0 ? 'complete' : ''}">
  <p>{task.TaskName}</p>
  <div>{task.IsClosed !== 0 ? <div>Completed</div> : null}</div>
</div>

Output:

<div class="boards {task.IsClosed !== 0 ? 'complete' : ''}">
  <p>Task 2</p>
  <div><div>Completed</div></div>
</div>

I want the output to be <div class="boards complete">

1 Answer 1

2

<div className=`boards ${task.IsClosed !== 0 ? 'complete' : ''}`>
  <p>{task.TaskName}</p>
  <div>{task.IsClosed !== 0 ? <div>Completed</div> : null}</div>
</div>

I think you want this^^^

for string interpolation in javascript the ` character must be used for quotes and the expression being interpolated must be within ${}

... or you can make a variable to store the class i.e.

let className = task.IsClosed !== 0 ? 'complete' : '';
...
...
<div className={className}>
  <p>{task.TaskName}</p>
  <div>{task.IsClosed !== 0 ? <div>Completed</div> : null}</div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I got it resolve with this code: className={boards__body${task.IsClosed !== 0 ? complete` : ''}`}

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.