I'm using React Router 4.
When I render component with render parameter componentWillReceiveProps() it doesn't fire the fist time, so I need to click twice to sent props to the component.
I render like this:
const CartRoute = (props) => (<Cart itemsInCart = {this.state.itemsInCart} deleteItemFromCart = {this.deleteItemFromCart} {...props} />);
.....
<Switch>
.....
<Route path="/cart" render={CartRoute} />
</Switch>
Without Router (when all components are on the same page) it works ok.
Here is detailed description:
React router - Need to click LINK twice to pass props to Component
I think Reason is simple one, As per DOC:
componentWillReceiveProps
will not get called when first time component get rendered, at that timecomponentDidMount
get called, when you do any changes in props values then onlycomponentWillReceiveProps
will get triggered. So first time if you want to do some calculation do that incomponentDidMount
method, like this:componentWillReceiveProps
is a Updating lifecycle method not Mounting method.Mounting Methods:
Updating Methods:
Check the difference between Mounting and Updating lifecycle method.