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

TypeError when parent Component is updated after uncaught exception in child Component's componentWillMount #3313

Closed
adamziel opened this issue Mar 4, 2015 · 2 comments

Comments

@adamziel
Copy link

adamziel commented Mar 4, 2015

Expected result: rendered text There was an error
Actual result: Uncaught TypeError: Cannot read property 'unmountComponent' of undefined
JSFiddle: http://jsfiddle.net/kb3gN/10167/

The code:

var Parent = React.createClass({
    getInitialState: function() {
        return {error: false};
    },
    componentWillMount: function() {
        var self = this;
        // window.onerror is only used for brevity
        window.onerror = function() {
            if(self.isMounted()) {
                self.setState({error: true});
            }
        };
    },
    render: function() {
        if(this.state.error) {
            return <div>There was an error</div>;
        } else {
            return <Child />;
        }
    }
});

var Child = React.createClass({
    componentWillMount: function() {
        throw new Error("test error");
    },
    render: function() {
        return <div>Child</div>;
    }
});

React.render(<Parent />, document.body);
@jimfb
Copy link
Contributor

jimfb commented Mar 5, 2015

Good catch. An explosion within a component should not cause the React core to die. cc @sebmarkbage

@jimfb jimfb added the Type: Bug label Mar 5, 2015
@sebmarkbage
Copy link
Collaborator

This is effectively a duplicate of #2461

Throwing during the render phase leaves React in a broken state and we're unable to recover. We need to do some major refactoring and introduce better regressions tests to support this. Will close this out and leave it to #2461

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

3 participants