freudianrudin
freudianrudin

Reputation: 1

Also it is better to use the callback option when using setLoggedIn in order to capture the previous version of the state as the following:

Also it is better to use the callback option when using setLoggedIn in order to capture the previous version of the state as the following:Also it is better to use the callback option when using setLoggedIn in order to capture the previous version of the state as the following:Also it is better to use the callback option when using setLoggedIn in order to capture the previous version of the state as the following:Also it is better to use the callback option when using setLoggedIn in order to capture the previous version of the state as the following:Also it is better to use the callback option when using setLoggedIn in order to capture the previous version of the state as the following:

Upvotes: 0

Views: 101

Answers (3)

Teddy Sterne
Teddy Sterne

Reputation: 14221

You are destructuring the state value and change handler incorrectly. It returns a tuple so you need to get the values like this:

const [loggedIn, setLoggedIn] = useState(false)

Upvotes: 0

norbitrial
norbitrial

Reputation: 15166

The problem is useState is returning with [] instead of {}.

You should have the following instead:

const [loggedIn, setLoggedIn] = useState(false);

+1 suggestion:

Also it is better to use the callback option when using setLoggedIn in order to capture the previous version of the state as the following:

const handleClick = () => {
   setLoggedIn(prev => !prev);
}

I hope this helps!

Upvotes: 1

Hadi Pawar
Hadi Pawar

Reputation: 1126

change const {loggedIn, setLoggedIn} = useState(false)

To : const [loggedIn, setLoggedIn] = useState(false)

Dont use {} to declare useState variable and its setter function use [] these instead.

Upvotes: 0

Related Questions