Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React / How to assign a variable to a classname property

Hello

In the following code I have tried to assign the variable classNameDiv to a classname, but it doesn’t work. Strangely double curly brackets works for the style property just below

return (
  <div className="item" key={i}>
    <img src={`./images/${myjson.images[index].name}`} alt="" />
    <div className="allBoxes">
      {myjson.images[index].palette.map((index2, j) => {
        const b = myjson.images[index].palette[j]
        const classNameDiv = "colorBox";
        if (RefArrayPalette[index][j] === 1) {
          classNameDiv = "colorBox2";//_________________first here___________
        }
        return (
          <div
            key={j}
            //className="colorBox"
            className={{classNameDiv}}  //____________________then here_____________
            style={{ backgroundColor: index2 }}
            onClick={calculateNewList}
          />
        )
      }, (i, index))}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In your example className is a const so it not reassignable, you should use let

div className props is expected to be a string

className={{classNameDiv}} actually mean you are passing the object { classNameDiv: classNameDiv } as className prop

What you should do is className={classNameDiv}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading