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

componentWillUnmount failing to fire #373

Closed
jakearchibald opened this issue Oct 26, 2016 · 7 comments
Closed

componentWillUnmount failing to fire #373

jakearchibald opened this issue Oct 26, 2016 · 7 comments
Assignees
Milestone

Comments

@jakearchibald
Copy link
Contributor

jakearchibald commented Oct 26, 2016

componentWillUnmount isn't called if:

  • The component in question is within another element in the render
  • The render changes to a component, rather than a regular element

Pop this in the repl:

export default class Foo extends Component {
  constructor(...args) {
      super(...args);
      this.state = {showTests: true};
    }
    componentDidMount() {
        setTimeout(() => {
            this.setState({showTests: false});
        }, 2000);
    }
    render({ }, { showTests }) {
        if (!showTests) return <AnotherTest/>;

        return (
            <div>
                <Test/>
            </div>
        );
    }
}

class Test extends Component {
  componentDidMount() {
    console.log('test mount');
  }

  componentWillUnmount() {
    console.log('test unmount');
  }

  render() {
    return <div>Test</div>;
  }
}

class AnotherTest extends Component {
  componentDidMount() {
    console.log('anothertest mount');
  }

  componentWillUnmount() {
    console.log('anothertest unmount');
  }

  render() {
    return <div>Another test</div>;
  }
}

Observe the log, componentWillUnmount does not get called on Test.

Replacing if (!showTests) return <AnotherTest/>; with if (!showTests) return <div><AnotherTest/></div>; is a workaround in the meantime.

@developit
Copy link
Member

Hi @jakearchibald - is it possible this is the same issue as #349? If so, I have a fix.

@jakearchibald
Copy link
Contributor Author

You tell me 😄 .

Rendering isn't broken in the above example, just a lack of componentWillUnmount. It could be the same issue.

@developit
Copy link
Member

developit commented Oct 26, 2016

Looking into it! Thanks for the simple repro.

@developit developit added the bug label Oct 26, 2016
@developit
Copy link
Member

Update: Found the source of the issue. Will publish a fix soon!

@jakearchibald
Copy link
Contributor Author

Thanks! No rush though, the workaround is working. Was it the same issue as #349?

@developit
Copy link
Member

No, though I think he might be running into this one now.

@jakearchibald
Copy link
Contributor Author

Cheers!

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

No branches or pull requests

2 participants