Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

WhatAKitty
Copy link
Contributor

@WhatAKitty WhatAKitty commented Dec 2, 2017

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 that caused by setting state while component has already been unmounted.

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.
Copy link
Member

@afc163 afc163 left a 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 }));
Copy link
Member

@afc163 afc163 Dec 2, 2017

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.

@ddcat1115 @nikogu

@WhatAKitty
Copy link
Contributor Author

@afc163 that warning is not related to setState in componentWillUnmount.
If you use local component and do this.setState, this warning would show sometimes. Such as change page quickly. The main reason is that the component is being unmount and the react is trying to setState at sametime

@afc163
Copy link
Member

afc163 commented Dec 2, 2017

@WhatAKitty
Copy link
Contributor Author

WhatAKitty commented Dec 2, 2017

@afc163 yeah, if you place all of your data in redux, make safe state is actually unnecessary.
Like @gaearon said in facebook/react#2787 (comment)

But how about React state(local component state) ?

@afc163
Copy link
Member

afc163 commented Dec 2, 2017

Same as global state, check the React post above.

The real problem is that we can't cancel dispatch().then() in componentWillUnmount due to the limitation of dva's API.

cc @sorrycc

@WhatAKitty
Copy link
Contributor Author

@afc163 gitter?

@gaearon
Copy link

gaearon commented Dec 2, 2017

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.

@WhatAKitty WhatAKitty closed this Dec 2, 2017
@WhatAKitty
Copy link
Contributor Author

@gaearon Thanks for your suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants