question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`componentWillReceiveProps()` does not trigger for new context changes

See original GitHub issue

The componentWillReceiveProps() method only triggers if props change. However, there are situations where the context changes, but the props do not, but this method does not trigger. It makes it really difficult to alter the state before an update, as we can’t do this in componentWillUpdate().

Perhaps a new method, like componentWillReceiveContext() can be added, or the previous componentWillReceiveProps() can simply be renamed to something like componentWillReceive(), which is always called if any prop/state/context has changed.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
yangshuncommented, Dec 31, 2015

In fact, componentWillReceiveProps does trigger when context changes. Here’s an example fiddle of how it can be used and accomplished:

https://jsfiddle.net/bzytthat/

Read more about using context in lifecycle methods: http://facebook.github.io/react/docs/context.html#referencing-context-in-lifecycle-methods

6reactions
milesjcommented, Dec 31, 2015

I did some debugging in the React source, and it seems to always trigger true for this clause. https://github.com/facebook/react/blob/3b96650e39ddda5ba49245713ef16dbc52d25e9e/src/renderers/shared/reconciler/ReactCompositeComponent.js#L657

Which seems to agree with my original issue, that the componentWillReceiveProps() isn’t being called because the props haven’t changed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

componentWillReceiveProps not firing when the state gets ...
So, I have an action and a reducer to pass the status of the button through. This is working fine except that the...
Read more >
componentWillReceiveProps Not Triggered After Mounting
componentWillReceiveProps isn't triggered after the node is put on scene. ... and acting upon changes; not triggering it at mounting (where there are...
Read more >
React.Component
Use shouldComponentUpdate() to let React know if a component's output is not affected by the current change in state or props. The default...
Read more >
Lifecycle deprecated/New Methods - React Training
A setState used in this function is “free” and will not trigger a re-render. DO ... whenever the props they receiver, their state...
Read more >
Class: MinimizeButton - Finsemble
Called when the component may be receiving new props. React may call this even if props have not changed, so be sure to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found