Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set ReactJS state asynchronously

If you do an asynchronous action that updates the state in componentWillMount (like the docs say), but the component is unmounted (the user navigates away) before that async call is complete, you end up with the async callback trying to set the state on a now unmounted component, and an

"Invariant Violation: replaceState(...): Can only update a mounted or mounting component."

error.

What's the best way around this?

Thanks.

like image 859
nicholas Avatar asked Jul 31 '14 19:07

nicholas


People also ask

How do I change state asynchronous?

To update the state of a component, you use the setState method. However it is easy to forget that the setState method is asynchronous, causing tricky to debug issues in your code. The setState function also does not return a Promise. Using async/await or anything similar will not work.

Is setState async in functional component?

setState is asynchronous for this reason, and React will batch setState calls inside of React based event handlers to reduce component re-renders.

How do I change my set state in React?

State can be updated in response to event handlers, server responses, or prop changes. This is done using the setState() method. The setState() method enqueues all of the updates made to the component state and instructs React to re-render the component and its children with the updated state.

Is useState synchronous or asynchronous?

TL;DR: useState is an asynchronous hook and it doesn't change the state immediately, it has to wait for the component to re-render. useRef is a synchronous hook that updates the state immediately and persists its value through the component's lifecycle, but it doesn't trigger a re-render.


1 Answers

You can use component.isMounted method to check if component was actually attached to the DOM before replacing its state. Docs.

isMounted() returns true if the component is rendered into the DOM, false otherwise. You can use this method to guard asynchronous calls to setState() or forceUpdate().

UPD: Before you downvote. This answer was given 2 freaking years ago. And it was the way to do stuff back that days. If you are just starting to use React do not follow this answer. Use componentDidMount or whatever another lifecycle hook you need.

like image 124
Yury Tarabanko Avatar answered Sep 23 '22 16:09

Yury Tarabanko