Video yükleniyor...

Video Yüklenemedi

Ana Sayfaya Dön

"Why can't I access an updated state value in React immediately?"

142,247 görüntüleme • 3 yıl önce •via X (Twitter)

9 Yorum

Alex Sidorenko profil fotoğrafı
Alex Sidorenko3 yıl önce

Because it's not how React works. Updating a state in React triggers a new render (React calls your component function again with a new state). You can access the updated state variable during this new render.

Shyam Makwana profil fotoğrafı
Shyam Makwana3 yıl önce

Other solution is to use callback function like:

Neo 🍍🍍 profil fotoğrafı
Neo 🍍🍍3 yıl önce

There’s a callback on setState if you need it immediately

Bilal Tamerhoulet 🇩🇿 🇵🇸 profil fotoğrafı
Bilal Tamerhoulet 🇩🇿 🇵🇸3 yıl önce

State updates are asynchronous

harpaljadeja.eth (evm/acc) 🇮🇳 profil fotoğrafı
harpaljadeja.eth (evm/acc) 🇮🇳3 yıl önce

So what's the solution?

🦭x profil fotoğrafı
🦭x3 yıl önce

Closure

Eberechi profil fotoğrafı
Eberechi3 yıl önce

React batches changes and updates only after the whole function is done running. Hence a log immediately after changing set only had access to the initial state.

nadim profil fotoğrafı
nadim3 yıl önce

What are you using to create this awesome code animation?

Claudio profil fotoğrafı
Claudio3 yıl önce

Nice content mate, but it never ceases to amaze me how weirdly architected React is.

Benzer Videolar