0

I am trying to use UseState. I know that to make sure it works instantly we have to use "useEffect". Code below logs the new values, but does not change it when it calls the prop.

 useEffect(() => {
    console.log(
      "updated",
      itemId,
      highlight,

    );
  }, [highlight, itemId]);

Thank you in advance!

EDIT:

Example that illustrates the problem:

 const [highlight, setHighLight] = useState("Old Value1"); 
const [itemId, setItemID] = useState("Old Value"); 
setItemID(New Value); 
setHighlight(New Value2);

 ((USEEFFECT LOGS "New Value" and "New Value2"))

 console.log(ItemId) == returns "Old Value" 

Function where I set the itemId and print it.

await setItemID(id);
console.log(itemId);


await printSkillChart(id, peer2);
skyboyer
  • 22,209
  • 7
  • 57
  • 64
Mohammad Saad
  • 127
  • 1
  • 11
  • ` does not change it when it calls the prop` What do you mean? When what calls the prop? – Avin Kavish Jun 08 '19 at 10:21
  • Example: const [highlight, setHighLight] = useState("Old Value1"); const [itemId, setItemID] = useState("Old Value"); setItemID(New Value) setHighlight(New Value2) ((USEEFFECT LOGS "New Value" and "New Value2")) console.log(ItemId) == returns "Old Value" – Mohammad Saad Jun 08 '19 at 10:28
  • where are you console logging item id? Can you edit the question with the full snippet? – Avin Kavish Jun 08 '19 at 10:29
  • I'm just logging it in the console. – Mohammad Saad Jun 08 '19 at 11:30
  • 1
    That's not a full code snippet. I know you are logging it in the console. What matters is WHEN. Show me your entire function component. – Avin Kavish Jun 08 '19 at 11:31
  • setter is internally called in async way. `useEffect` is called in async way too and it's guaranteed it to be called after all setters been called. – skyboyer Jun 29 '19 at 13:51
  • Possible duplicate of [useState set method not reflecting change immediately](https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately) – skyboyer Jun 29 '19 at 13:53

0 Answers0