Home > Enterprise >  How to fix problem of same keys in same element using twice?
How to fix problem of same keys in same element using twice?

Time:11-20

I have a component which I use twice: so React read its keys as a same value:

App.tsx

<>
    <Module/>
    <Module/>
</>

Module.tsx

{props.advantages.map((i, idx) =>
    <div key={idx}>
        {i}
    </div>
)}

CodePudding user response:

Currently I'm learning React Native but this is my alternative:

App.tsx

 <>
    <Module type="not"/>
    <Module type="the-best"/>
    <Module type="alternative"/>
 </>

Module.tsx

{props.advantages.map((i, idx) =>
    var newKey = `${props.type}-${idx}`
    <div key={newKey}>
        {i}
    </div>
)}

Best regards.

  • Related