diff --git a/packages/react-router/modules/Router.js b/packages/react-router/modules/Router.js index a68b44d883..ab025d8070 100644 --- a/packages/react-router/modules/Router.js +++ b/packages/react-router/modules/Router.js @@ -20,6 +20,17 @@ class Router extends React.Component { router: PropTypes.object.isRequired }; + static getDerivedStateFromProps(nextProps) { + const { children } = nextProps; + + invariant( + children == null || React.Children.count(children) === 1, + "A may have only one child element" + ); + + return null; + } + getChildContext() { return { router: { @@ -46,13 +57,9 @@ class Router extends React.Component { }; } - componentWillMount() { - const { children, history } = this.props; - - invariant( - children == null || React.Children.count(children) === 1, - "A may have only one child element" - ); + constructor(props) { + super(props); + const { children, history } = props; // Do this here so we can setState when a changes the // location in componentWillMount. This happens e.g. when doing @@ -64,9 +71,9 @@ class Router extends React.Component { }); } - componentWillReceiveProps(nextProps) { + componentDidUpdate(prevProps) { warning( - this.props.history === nextProps.history, + this.props.history === prevProps.history, "You cannot change " ); }