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

376
Views
How to call a function containing state change from target.value to useEffect() function?

I have an error Cannot read properties of undefined (reading 'target') for this code:

const [title, setTitle] = useState("");

const handleChangeTitle = (e) => {
  setTitle(e.target.value);
};

useEffect(() => {
  handleChangeTitle()
}, [title]);

return (
  <div className="App">
    <input
      name="title"
      type="text"
      value={title}
      onChange={handleChangeTitle}
    />
    <p>{title}</p>
  </div>
);

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Remove the useEffect altogether. useEffect is for lifecycle events such as on initial render. What you're doing is when the input changes you update your state. That's fine call handleChangeTitle exactly as you are doing. However, your useEffect has a dependency on the title variable. That means anytime it changes it will fire every time that variable's value changes.

input -> value change -> handleChangeTitle updates state "title" -> useEffect fires -> calls handleChangeTitle AGAIN -> cycle over and over until app crashes

Besides the already called out missing parameter to handleChangeTitle there is nothing wrong with your code. It's working as intended. It's just flawed in its design. I would suggest reading up on the correct usage of each hook here

about 4 years ago · Santiago Trujillo Report

0

you are already watching [title] inside you useEffect , whenever onChange event fired the function will called , as Szymon Said you don't your onChange inside your useEffect

about 4 years ago · Santiago Trujillo Report
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!