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

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.

const tabRef = useRef();

  function addButtonHandler() {
    console.log(tabRef.current.innerText);
  
  }

<ul ref={tabRef}>
    <li >
      <button onClick={addButtonHandler}></button>
      Some context here.</li>
      <li >
      <button onClick={addButtonHandler}"></button>
      More context here.</li>
      <li >
      <button onClick={addButtonHandler}></button>
      even more context here.</li>
    </ul>

>Solution :

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

This seems like a good candidate for event delegation. Also, you don’t necessarily need a ref here just to get the value, because 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.

const App = () => {

  function handleClick(event) {
    event.preventDefault();
    if (event.target.tagName === 'BUTTON') {
      console.log(event.target.nextSibling);
    }
  }

  return (
  <ul onClick={handleClick}>
    <li>
      <button>Button 1</button>
      <span>Some context here.</span>
    </li>
    <li>
      <button>Button 2</button>
      <span>More context here.</span>
    </li>
    <li >
      <button>Button 3</button>
      <span>even more context here.</span>
    </li>
    </ul>)
    }
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>
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