0

I have var in my state

const [currentNodef, setCurrentNodef] = React.useState();

and the function that is called by the button

const  openModalUpdateNode= (rowInfo)=>{
   const { node, treeIndex, path } = rowInfo;
   setCurrentNodef(node);
   console.log(currentNodef)
}

On the first click I get an undefined, on the second click the previous value.

As far as I understand, I need to wait for the state to be updated before reading it, but experimenting with async/await didn't bring the desired result.

Tell me, what could be the matter?

Thomazzz
  • 193
  • 2
  • 10
  • correct your question, `async/away` to `async/await` – Rahul Jul 14 '21 at 07:04
  • Rahul, o, thank you) – Thomazzz Jul 14 '21 at 07:06
  • 1
    `setCurrentNodef` isn't an `async` function nor does it return a Promise, it can't be awaited, or rather, waiting for it will have no effect. When you console log the state *right after* enqueueing an update you are logging the state value from the ***current*** render cycle, not what it will be on the ***next*** render cycle. Use an `useEffect` with appropriate dependency on state to log the update. – Drew Reese Jul 14 '21 at 07:07
  • Drew Reese, Thanks for the clarification, I understood everything – Thomazzz Jul 14 '21 at 07:11

0 Answers0