T
T
tron212021-09-07 15:11:38
React
tron21, 2021-09-07 15:11:38

What is the correct way to use setstate in a functional component to change a boolean value?

Good afternoon everyone! I'm making a React project. when executing a function (authorization function) I need to change the boolean value in this code.

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


You need to change it so that the boolean value changes to true immediately, and not when the page is reloaded. This is necessary to correctly obtain information about the user. (I get the effect of getting information about the user if loggedIn = true) .

Can you please tell me how to correctly write the function callback in this setState to complete my task? Thank you in advance for your help. I looked at the documentation, but there are class components and the solution was slightly incomprehensible to me. Sorry if the question is too simple. Addendum: Yes, I remembered useEffect. I will correct the code in my application as it is necessary for this to work correctly. But the question remains the same. How can I add a callback function to setState if it is a functional component? Thanks in advance!

setLoggedIn(true);


Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question