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

Bug: shouldComponentUpdate() doesn't being called while using this.context #18433

Closed
BiosBoy opened this issue Mar 30, 2020 · 2 comments
Closed

Comments

@BiosBoy
Copy link

BiosBoy commented Mar 30, 2020

Well, guys, I've read this post by @gaearon #2517 and still didn't figure out - does new context API is compatible with shouldComponentUpdate() since React v.16?

As far as I tested today shouldComponentUpdate is not callable at all while using this.context on the child component.

React version:

React & ReactDOM: v.16.13.1

Steps To Reproduce

  1. Create a basic wrapper component with its child like: <MyContext.Provider value={store}>{child}</MyContext.Provider>
  2. Set context on the child: static contextType = MyContext.
  3. Try to call at least some console.log() inside ShouldComponentUpdate(), 100% the last one won't event being called.

The current behavior

Child component re-renders every time, shouldComponentUpdate even don't call at all.

The expected behavior

shouldComponentUpdate should be callable while used along with this.context on the child components.

UPD

I found shouldComponentUpdate working while using context on the child through <MyContext.Consumer> via HOC? That's the magic or a real bug...

@BiosBoy BiosBoy added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Mar 30, 2020
@BiosBoy BiosBoy changed the title Bug: ShouldComponentUpdate doesn't being called when using this.context Bug: shouldComponentUpdate doesn't being called when using this.context Mar 30, 2020
@BiosBoy BiosBoy changed the title Bug: shouldComponentUpdate doesn't being called when using this.context Bug: shouldComponentUpdate() doesn't being called while using this.context Mar 30, 2020
@aweary
Copy link
Contributor

aweary commented Apr 13, 2020

Hey @BiosBoy! It's exepcted that shouldComponentUpdate isn't called when context is updated. This is noted in the context docs:

The propagation from Provider to its descendant consumers (including .contextType and useContext) is not subject to the shouldComponentUpdate method, so the consumer is updated even when an ancestor component skips an update.

Hope that helps clear things up!

@aweary aweary closed this as completed Apr 13, 2020
@aweary aweary added Resolution: Invalid Resolution: Expected Behavior and removed Resolution: Invalid Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Apr 13, 2020
@Zhell1
Copy link

Zhell1 commented Apr 18, 2020

I am having the same issue, I have react automatically rendering too much stuff when I update the context, where sometime only 1 consumer has changed. Is there any way to prevent propagation to consumers in some cases ?
update: answer is yes, see #15156

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