0

So i've been trying to use event.target.name.value for an input field but when i submit the form, the values are null and i have to click twice to get the values. Here is my code:

const [name, setName] = useState('');
const handleSubmit = (event)=> {
  setName = event.target.name.value;
  console.log(name);
}

What am i doing wrong?

Jackmekiss
  • 633
  • 5
  • 12
Rawson
  • 1
  • 1
  • `setName = event.target.name.value;` causes an exception, as `setName` is a constant. Did you mean `const name = event.target.name.value;`? Or `setName(event.target.name.value);`? – Bergi May 08 '22 at 15:16
  • Most likely a duplicate of [The useState set method is not reflecting a change immediately](https://stackoverflow.com/q/54069253/1048572) – Bergi May 08 '22 at 15:30

2 Answers2

0

You are setting the function which is wrong. you need to pass it as parameter to setName function.

const [name, setName] = useState('');
console.log(name);
const handleSubmit = (event)=> {
  setName(event.target.value);
  
}
urchmaney
  • 618
  • 5
  • 7
-1

brother event.target.name.value it will take the value from your targeted input like name is a

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 10 '22 at 09:14