Home > Software design >  How to get attribute value from onClick inside functional component (React)
How to get attribute value from onClick inside functional component (React)

Time:04-22

What I need to do is access the value of an html attribute inside onclick event. This is my code.

const component = () => {

   handleClick = value => {
      console.log(value);
   }

   return (
      <div>
         <button value="value" onClick={(e) => handleClick(e.currentTarget.value)}>
             Click
         </button>
      </div>
   )
}

But unfortuately the value comes as undefined.

CodePudding user response:

Here is a code snippet which is working as expected.

https://codesandbox.io/s/happy-moser-ufqi9j?file=/src/App.js

  • Related