Next.js Discord

Discord Forum

How To Access The key attribute?

Unanswered
Raymond posted this in #help-forum
Open in Discord
I have the following element from which I want to print the key (randomly generated)

<li key={Math.floor(Math.random() * 1000000)} draggable={true} onClick={(event) => {
    console.log(event.currentTarget.key); // this doesn't work
}}>{item.name}</li>

How can I access the key value?

3 Replies

you don't access the key...
you generate the id of your item and then use the item.id as the key
you also pass the item to the prop so that you can also access the .id inside the component