pDotJoe
pDotJoe

Reputation: 1

Trying To understand Set State of React - Set State and apply it on single component

NOTE: Its App.js component

I have a State

state = {

  contacts: [...data is coming from an api],
  bgColors: ["bg-red","bg-blue",...so On]

  // bg colors array consist of class coming from css

}

inside render function->

I Used this.state.contacts.map to display data from contacts(state)

inside map function there is a div with className

<div className={`col-sm-3 relative book  ${Here I want to display bgColors String one by one} `}>

Upvotes: 0

Views: 37

Answers (1)

somethinghere
somethinghere

Reputation: 17350

Just use the native Array.join method to generate a string of classes:

<div className={`col-sm-3 relative book ${this.state.bgColors.join(' ')} `} />

Upvotes: 1

Related Questions