Search code examples
javascriptreactjslifecycle

componentWillReceiveProps isn't called until component is focused, is it something I've done?


I have a ReactJS component that I'm developing which needs to get some information from the URL. To do this I've been utilizing the componentWillReceiveProps lifecycle method to look at props.location.query and dispatches actions to a Redux store in order to update the known state. The problem that I'm running into is that it doesn't appear that componentWillReceiveProps is being called for my component until that component has focus. I can reload the page which hosts this component and the render is called but componentWillReceiveProps is never called first.

Clearly I'm either understanding the lifecycle incorrectly with respect to componentWillReceiveProps or there is something else either wrong in my component or application which is make it behave differently than it is intended. Please set me straight either way.


Solution

  • componentWillReceiveProps isn't called on the first render.

    From the React Docs:

    "Invoked when a component is receiving new props. This method is not called for the initial render."