0

Say I have two arrays like this:

state = {
   firstArr: ['one', 'two', 'three', 'four', 'five'],
   secondArr: ['one', 'three', 'four']
}

and I'm having a list of buttons like below:

{firstArray.map((item, index) => (
    <label className="btn btn-secondary" key={index}>
        <input type="checkbox" onChange={this.handleAddItem} value={item} /> {item}                              
    </label>
))}

How do I apply a different background color only to the buttons listed in the second array?

1 Answer 1

2

You need to have a conditional class like this, based on whether the current item is part of secondArr or not.

{firstArray.map((item, index) => (
    <label
        className={`btn btn-secondary ${secondArr.includes(item) ? 'different-bg-class' : ''}`}
        key={index}
    >
        <input type="checkbox" onChange={this.handleAddItem} value={item} /> {item}                              
    </label>
))}
Sign up to request clarification or add additional context in comments.

Comments

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.