Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
How to get clicked element innerText as a state with React useState hook

I was trying to get innterText/Menu Title with onclick event in React. It's getting the menu title properly but every time it's getting clicked it's just putting the same title again and again. (i.e. Menu Title Menu Title) how can I prevent this? Thanks in advance.

const [menuName, setMenuName] = useState()

const handleMenuName = (event) => {
    setMenuName(event.target.innerText)
  }

console.log(menuName) //output Menu Title Menu Title Menu Title...

<div onClick={(event) => handleMenuName(event)}>Menu Title</div>

I have tried like this but not working

 const handleMenuName = (event) => {
    const menusName = event.target.innerText
    setMenuName((prevState) => {
      console.log(prevState === menusName)
      if (prevState === menusName) return
      return menusName
    })
  }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!