Skip to content

Removed context masking to allow pure intermediate components to implement a context aware shouldComponentUpdate - #7213

Closed
bvella wants to merge 2 commits into
react:15-devfrom
ixaris:15-dev
Closed

bvella wants to merge 2 commits into
react:15-devfrom
ixaris:15-dev

Conversation

@bvella

@bvella bvella commented Jul 7, 2016 •

Copy link
Copy Markdown

I am using context to propagate locale and routing information. My problem is that some pure components stop subtree rendering when the context changes (because pure components only check state and props). This is a problem for many people as per issue #2517.

I reasoned that not masking the context for intermediate components would be better than masking, and the defined contextTypes would only be used for validating, not for filtering. This is similar to how propTypes work, where the properties specified are validated but the ones not specified are still available to the component. i have updated the tests to reflect this update.

Since components that are not interested in context would typically not use the context argument, not masking the context would not cause any existing component to break, unless said components explicitly are checking that the context param is undefined. This would, however, create an opportunity for a refinement of pure component, say a ContextAwarePureComponent which would compare state, props and context in shouldComponentUpdate, allowing context updates to cause re-renders of pure components.

I am currently running in development with this patch, using radium and some react-bootstrap components, and did not experience any issue

@ghost

ghost commented Jul 7, 2016

Copy link
Copy Markdown

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at [email protected]. Thanks!

@bvella bvella changed the title Removed context masking to allow pure intermediate components to implements a context aware shouldComponentUpdate Removed context masking to allow pure intermediate components to implement a context aware shouldComponentUpdate Jul 7, 2016
@jquense

jquense commented Jul 7, 2016

Copy link
Copy Markdown
Contributor

While I'm not sure this solution will pass muster, it is elegant in its way. My hunch is teh team will not like the leakiness of passing all context (requested or not) to each component. However it does solve the issue succinctly...

Why not pass the unmasked context only to SCU, at least that limits the use of context to a particularly small and readonly place.

@bvella

bvella commented Jul 7, 2016 •

Copy link
Copy Markdown
Author

For properties, components define propTypes, but they can still get properties that they do not define. In fact this is the basis for a number of generic Higher Order Components, as they pass along all properties to a wrapped component, whether they know about these properties or not.

The same reasoning can be applied to contexts, whereby through contextTypes, a component defines what it expects to be in the context, but not implying that nothing else can be in the context.

@zpao

zpao commented Jul 7, 2016

Copy link
Copy Markdown
Contributor

I haven't looked at this at all but if we did want to do something like it, we'd need to do it against the master branch (which means you'll need a new PR). Don't bother yet but just a heads up that it'll need to happen if we decide to take this.

@ghost

ghost commented Jul 12, 2016

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@bvella

bvella commented Jul 14, 2016

Copy link
Copy Markdown
Author

I applied this update to master as well: #7225

@traviscooperjs

Copy link
Copy Markdown

This is great!

  1. Syncing up how context works with how props work ( with regards to propTypes) is awesome!
  2. Also, as you mentioned, makes it so intermediate components do not need to know which context props any of its children may need.

Exit Question: Would this make context a supported ( and documented ) feature of React?

Cross Posted with #7225
#7225 (comment)

@zpao

zpao commented Jul 14, 2016

Copy link
Copy Markdown
Contributor

Closing out since you made the other one against master. Discussion can happen there.

@zpao zpao closed this Jul 14, 2016
@bchenSyd

bchenSyd commented Feb 1, 2017

Copy link
Copy Markdown

I agree.

Many HOC has its implementation of shouldComponentUpdate. The currently implementation of context won't work with them as not contextTypes can be specified in HOC, and hence they won't get propagated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants