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

bug componentWillUnmount is called before componentDidMount #5719

Closed
istarkov opened this issue Dec 22, 2015 · 8 comments
Closed

bug componentWillUnmount is called before componentDidMount #5719

istarkov opened this issue Dec 22, 2015 · 8 comments

Comments

@istarkov
Copy link
Contributor

React 0.14.3
componentWillUnmount is called before componentDidMount, but this must be impossible.

As additional check: calling findDomNode inside componentDidMount causes Invariant Violation: findDOMNode was called on an unmounted component.

code example and output jsbin example

import React, { Component } from 'react';
import { render, findDOMNode } from 'react-dom';

let buggyUid_ = 0;

class BuggyBehavior extends Component {
  constructor() {
    super();
    this.buggy = buggyUid_++;
  }

  componentDidMount() {
    console.log('didMount', this.buggy);
    findDOMNode(this);
  }

  componentWillUnmount() {
    console.log('willUnmount', this.buggy);
  }

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

class WillReceiveUpdater extends Component {
  componentWillReceiveProps(nextProps) {
    if (nextProps.interval[nextProps.prop] !== 3) {
      nextProps.setInterval({...nextProps.interval, ...{[nextProps.prop]: 3}});
    }
  }

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

class Main extends Component {
  constructor() {
    super();
    this.uid_ = 0;
    this.state = {
      data: [],
      interval: {a: 0, b: 0},
      uid: 0,
    };
  }

  componentDidMount() {
    setTimeout(() => this.setState({data: [1, 2, 3], uid: this.uid_++}), 1);
  }

  setInterval = (value) => {
    this.setState({interval: value, uid: this.uid_++});
  }

  render() {
    return (
      <div>
        <BuggyBehavior key={this.state.uid} />
        <WillReceiveUpdater
          prop={'a'}
          interval={this.state.interval}
          setInterval={this.setInterval}
        />
        <WillReceiveUpdater
          prop={'b'}
          interval={this.state.interval}
          setInterval={this.setInterval}
        />
      </div>
    );
  }
}

const App = () => <Main />;

render(
  <App />,
  document.getElementById('root')
);

code output of example above

// didMount 0
// willUnmount 0
// willUnmount 1 !!! WAT didMount is not called yet
// didMount 1
// invariant.js:39 Uncaught Error: Invariant Violation: findDOMNode was called on an unmounted component.
@chicoxyzzy
Copy link
Contributor

Just tried this example and I can confirm this behaviour exists. Is it expected behaviour to prevent memory leaks and clean something from constructor even if component is not mounted yet?

@jimfb
Copy link
Contributor

jimfb commented Dec 23, 2015

cc @sebmarkbage @spicyj

@sophiebits
Copy link
Collaborator

This might be #2410.

@sompylasar
Copy link
Contributor

@istarkov @chicoxyzzy @spicyj The related issue #2410 has been closed, should this issue be closed, too (if stopped reproducing)?

@sophiebits
Copy link
Collaborator

The jsbin is broken in 15.0.2 and fixed in 15.1.0 so almost certainly that, thanks.

@romulof
Copy link

romulof commented May 26, 2017

I'm experiencing this issue with React 15.5.4 while using React-Modal.

@gaearon
Copy link
Collaborator

gaearon commented May 26, 2017

If you're experiencing an issue please create an example reproducing it. Then we can diagnose it. Thanks!

@romulof
Copy link

romulof commented May 26, 2017

Sorry, I forgot to attach the example link.

But forget it, the bug was caused by async-reactor incorrect error handling putting React in a erroneous state.

gesuvskn added a commit to gesuvskn/noeldelgadoj that referenced this issue Dec 5, 2021
In some cases, `componentWillUnmount` is called before `componentDidMount`, which leaves `this.scrollbar` as null and causes a `Cannot read property 'destroy' of null` error. [See this issue in React](facebook/react#5719)
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

No branches or pull requests

7 participants