-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do safe state instead of setState #338 #342
Conversation
If you use this.setState in local component you should set @safestate and always use `this.props.setStateSafe` instead of native method `this.setState`, it will avoid `Can only update a mounted or mounting component` warning beacuse of setting state while component has already been unmounted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can only update a mounted or mounting component
We should clear all async setState
calls in componentWillUnmount instead of make a safe setState.
@@ -36,7 +38,7 @@ export default class Analysis extends Component { | |||
componentDidMount() { | |||
this.props.dispatch({ | |||
type: 'chart/fetch', | |||
}).then(() => this.setState({ loading: false })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use dispatch().then()
, move the logic into sagas.
@afc163 that warning is not related to |
@afc163 yeah, if you place all of your data in redux, make safe state is actually unnecessary. But how about React state(local component state) ? |
Same as global state, check the React post above. The real problem is that we can't cancel cc @sorrycc |
@afc163 gitter? |
It’s not a great way of doing it IMO. When you have a cancelable API, use it. If the API is not cancelable, you can create a cancelable wrapper for it, and use that wrapper. In either case, patching this on the component level ignores the real problem: APIs that don’t let you cancel. |
@gaearon Thanks for your suggestions. |
If you use this.setState in local component you should set @safestate and always use
this.props.setStateSafe
instead of native methodthis.setState
, it will avoidCan only update a mounted or mounting component
warning that caused by setting state while component has already been unmounted.