Skip to content

Avoid setState on unmounted components #97

Open

Description

@srph

So, I'm using react-resolver with react-router for permissions. So, if a user doesn't pass a certain requirement, they're redirected somewhere. This means that I don't really want to put something to the props.

I just want to avoid errors such as these:

Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Resolver component.
Warning: setState(...): Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.

Activity

  1. ratson commented on Jan 2, 2016

    @ratson
    Contributor

    Encountered the same problem, @ericclemmons is there any way we could avoid it?

    Update:
    Just have read https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
    Seems it is the solution that react-resolver could implement to solve the problem.

  2. ericclemmons commented on Jan 2, 2016

    @ericclemmons
    Owner

    I'm curious if this is solved in react-router 2.0.

    This exists because resolution happens, you attempt to redirect (which should be synchronous and immediate), but the router seems to trigger the redirect in the next event loop/tick/whatever, meaning the component still renders, mounts, etc. and then the redirect happens.

    I'm all for a fix, but curious if this has been solved with the rewrite first.

  3. ratson commented on Jul 9, 2016

    @ratson
    Contributor

    This line could be executed after unmount regardless redirects, as promise is resolved asynchronously.

    AFAIK, that's why the blog post suggest to cancelling the promise before unmount. Or am I missing something?

  4. ericclemmons commented on Jul 9, 2016

    @ericclemmons
    Owner

    I suppose the only/best way around this is to track if the component is still mounted or not before calling setState...

  5. changed the title [-]Plain Resolver[/-] [+]Avoid setState on unmounted components[/+] on Jul 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions