Skip to content
Advertisement

Get value inside unordered list using React UseRef

Hi I have the below code, in which I am trying to get the value inside list using when a person clicks on the button wrapped inside that is inside list element. I tried using UseRef but it is returning all the listed items, but In my case I only want to target the value that is associated with that button. Here is my current approach.

JavaScript

Advertisement

Answer

This seems like a good candidate for event delegation. Also, you don’t necessarily need a ref here just to get the value, because JavaScript event handlers provide you with the event object, which includes the target of the event! You could certainly use a ref to store it for later, but my point here is that useRef is not a necessary part of getting a value from a list in an event handler. No “Reacty” features needed at all here!

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement