Skip to main content
deleted 39 characters in body
Source Link

I too was stuck with the same problem. As other answers above have clarified the error here, which is that useState is asynchronous and you are trying to use the value just after setState. It is not updating on the console.log() part because of the asynchronous nature of setState, it lets your further code to execute, while the value updating happens on the background. Thus you are getting the previous value. When the setState is completed on the background it will update the value and you will have access to that value on the next render.

If anyone is interested to understand this in detail. Here is a really good Conference talk on the topic.

https://www.youtube.com/watch?v=8aGhZQkoFbQ

I too was stuck with the same problem. As other answers above have clarified the error here, which is that useState is asynchronous and you are trying to use the value just after setState. It is not updating on the console.log() part because of the asynchronous nature of setState, it lets your further code to execute, while the value updating happens on the background. Thus you are getting the previous value. When the setState is completed on the background it will update the value and you will have access to that value on the next render.

If anyone is interested to understand this in detail. Here is a really good Conference talk on the topic.

https://www.youtube.com/watch?v=8aGhZQkoFbQ

As other answers above have clarified the error here, which is that useState is asynchronous and you are trying to use the value just after setState. It is not updating on the console.log() part because of the asynchronous nature of setState, it lets your further code to execute, while the value updating happens on the background. Thus you are getting the previous value. When the setState is completed on the background it will update the value and you will have access to that value on the next render.

If anyone is interested to understand this in detail. Here is a really good Conference talk on the topic.

https://www.youtube.com/watch?v=8aGhZQkoFbQ

added 10 characters in body
Source Link
Apostolos
  • 10.5k
  • 5
  • 33
  • 44

I too was stuck with the same problem. As other answers above have clarified the error here, which is that useStateuseState is asynchronous and you are trying to use the value just after setStatesetState. It is not updating on the console.log()console.log() part because of the asynchronous nature of setStatesetState, it lets your further code to execute, while the value updating happens on the background. Thus you are getting the previous value. When the setStatesetState is completed on the background it will update the value and you will have access to that value on the next render.

If anyone is interested to understand this in detail. Here is a really good Conference talk on the topic.

https://www.youtube.com/watch?v=8aGhZQkoFbQ

I too was stuck with the same problem. As other answers above have clarified the error here, which is that useState is asynchronous and you are trying to use the value just after setState. It is not updating on the console.log() part because of the asynchronous nature of setState, it lets your further code to execute, while the value updating happens on the background. Thus you are getting the previous value. When the setState is completed on the background it will update the value and you will have access to that value on the next render.

If anyone is interested to understand this in detail. Here is a really good Conference talk on the topic.

https://www.youtube.com/watch?v=8aGhZQkoFbQ

I too was stuck with the same problem. As other answers above have clarified the error here, which is that useState is asynchronous and you are trying to use the value just after setState. It is not updating on the console.log() part because of the asynchronous nature of setState, it lets your further code to execute, while the value updating happens on the background. Thus you are getting the previous value. When the setState is completed on the background it will update the value and you will have access to that value on the next render.

If anyone is interested to understand this in detail. Here is a really good Conference talk on the topic.

https://www.youtube.com/watch?v=8aGhZQkoFbQ

Source Link

I too was stuck with the same problem. As other answers above have clarified the error here, which is that useState is asynchronous and you are trying to use the value just after setState. It is not updating on the console.log() part because of the asynchronous nature of setState, it lets your further code to execute, while the value updating happens on the background. Thus you are getting the previous value. When the setState is completed on the background it will update the value and you will have access to that value on the next render.

If anyone is interested to understand this in detail. Here is a really good Conference talk on the topic.

https://www.youtube.com/watch?v=8aGhZQkoFbQ