0

Why are my 2 console.log different ?

https://codesandbox.io/s/modest-colden-ib87m?file=/src/App.js

value is ok but login misses one letter every time.

turn 1: value is "a" when login is ""

turn 2: value is "ab" when login is "a"

etc ...

thanks for help !

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Jim
  • 669
  • 4
  • 10
  • 24
  • Hey, could you try 'onkeyup' event instead of 'change'? – Halil İbrahim Özdoğan Oct 25 '20 at 20:52
  • Nothing happens – Jim Oct 25 '20 at 20:56
  • if you provide sandbox code, It will help more – Halil İbrahim Özdoğan Oct 25 '20 at 21:00
  • there isn't enough code to reproduce your issue, but my guess is that you are assuming that `setForm` is instantaneous, which it isn't, so `form` still holds its previous value. – Sheraff Oct 25 '20 at 21:02
  • https://codesandbox.io/s/modest-colden-ib87m?file=/src/App.js here it is. So how can it be instantaneous ? – Jim Oct 25 '20 at 21:07
  • It can't. React state updates are asynchronous. They are enqueued during the render cycle, batch processed between render cycles, and available during the next render cycle. Is there something other than log the next state value that you are *really* trying to do? – Drew Reese Oct 25 '20 at 21:18
  • Yes, i'd like to check if the input is good or flase (with regex for mail for example) So i need to have tu current state to be updated when i send it to my checkMailFunction(). Maybe i miss something ? – Jim Oct 25 '20 at 21:21
  • You can either check the input value *before* updating state, or use an `useEffect` hook to check form state *after* it has updated. If your issue *isn't* react state synchronicity then please update with a more complete code example and what isn't working and click reopen. – Drew Reese Oct 25 '20 at 21:26
  • I updated the post, im going to try to use useEffect then but i don't use it yet, i'm new in react hooks – Jim Oct 25 '20 at 21:31

0 Answers0