I'm making a simple to-do app with add and delete functionality and I want to implement an undo-delete option.
So far, I've tried using context but I have a problem with context state logic: undo function uses not-yet updated state values, which leads to errors.
The problem is better documented in the demo:
EDIT: I eventually solved the problem by discarding the setTimeout logic and using useReducer instead of useEffect, as shown in this example:
The problem is when you update your states values and then run your setTimeout, the previous values are used in setTimeout not the new ones.
Because setTimeout uses the values at the time that it is created. Read more here.
So you have 2 options to fix your issue.