skip to Main Content

I have

const [setEmail, email] = useState();

though when I call setEmail("[email protected]") it does not setEmail and it does not give an error message

code

alert("before email - yes)

setEmail([email protected])

alert("after email - no)

alert("after email - no) never gets called? what the actual , no error message given either. and its not to do with @.com as when i call setEmail("sdsafs") it doesn’t work either.

2

Answers


  1. I think the way you are using the useState hook is wrong. What does useState return? It returns a pair of values: the current state and a function that updates it. This is why we write const [count, setCount] = useState().The order is important here. You have named them the other way. try below,

    const [email, setEmail] = useState();
    

    also refer https://legacy.reactjs.org/docs/hooks-state.html for how to use useState hook

    Login or Signup to reply.
  2. You should use useState hook properly. And it is not correct in the use of alert.

    alert("before email - yes");
    const [email, setEmail] = useState("");
    alert("after email - no");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search