U
U
uRoot2020-12-24 11:02:05
React
uRoot, 2020-12-24 11:02:05

Why is React complaining about missing keys in a normal JSX chunk?

React throws an error: "Warning: Each child in a list should have a unique "key" prop" on a normal piece of JSX. Don't tell me why?

There is a component:

{
        data.map((element) =>
          <Card
            key={element.key}
            img={element.img}
            name={element.name}
            handleToggle={element.action()}
            hoverTitle={element.hoverTitle}
            desk={element.desk}
            btnText={element.btnText}
            btmMod={element.btmMod}
            imgMod={element.imgMod}
            bgMod={element.bgMod}
          />
        )
      }


Card Component:
spoiler
<div
      className={"card " + (bgMod)}
      ref={cardWrapper}
    >
      <div
        className={"card__nav card--zIndex"}
        onClick={() => handleToggle(cardWrapper)}
      >
        <svg></svg>
      </div>
      <div className={"card__img " + (imgMod)}>
        <img alt={name} src={img} />
      </div>
      <div className={"card__name card--zIndex"}>
        {name}
      </div>
      <div className={"card__desk"}>
        {desk}
      </div>
      <div className={"card__hover-title"}>
        {hoverTitle}
      </div>
      <Button
        mod={btmMod}
        text={btnText}
      />
        <svg></svg>
      <div className={"card__inner"} />
    </div>


If you remove {hoverTitle} in the Card component, the error will disappear. Do not tell me how to fix this and why this error appears? After all, I always thought that it was only necessary to specify keys when generating elements, but this is clearly not the case here.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question