I am trying to set localStorage equal to current state after it is updated. Unfortunately, I notice that localStorage set to prevState value. Is there any way to make sure localStorage updates after setState?
I have some months without working with React but if a remember good, the setState method is asynchronous so you have to wait until the component is updated. That’s why the React component class have life cycle methods.
I think the best method for this is using the built in componentDidUpdate method. Does as the method is named, when your component renders from a prop/state change it’s going to be called.
Here’s an example from the react course I’ve been doing.