RickWong / react-transmit

Relay-inspired library based on Promises instead of GraphQL.
BSD 3-Clause "New" or "Revised" License
1.32k stars 61 forks source link

Guard setState so that it is not called when the container is already unmounted #36

Closed leonid-shevtsov closed 9 years ago

leonid-shevtsov commented 9 years ago

This is done to silence a warning in React:

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component CalculatorContainer. This is a no-op.

See discussion at https://github.com/facebook/react/issues/2787

As this issue is unresolved and there is no final opinion from React devs, I think it's prudent to add a guard for the time being.

RickWong commented 9 years ago

Haven't followed the official discussion. Is the "Note" about ES6 classes on https://facebook.github.io/react/docs/component-api.html#ismounted not relevant anymore? @leonid-shevtsov

leonid-shevtsov commented 9 years ago

It is relevant, and when you create an ES6 class that extends React.Component it doesn't have isMounted. But since you are creating the containers with React.createClass, you're fine.

Transmit containers with this patch interop with ES6 classes as usual.

(It's the ES6 classes themselves that, in a similar situation, would have to resort to checking a flag set in componentWillUnmount() or something like that. I hope that in the future the React team will work out a better solution.)