Skip to content

Switch the Context to use the Parent tree instead of the Owner tree #2112

@sebmarkbage

Description

@sebmarkbage
Collaborator

I'm pretty convinced at this point that Contexts are more useful in the Parent tree than the Owner tree. It also opens up new use cases for parent->child communication that wasn't possible before.

Activity

1st1

1st1 commented on Sep 30, 2014

@1st1

I'd be a big +1 for this. This would really make it much easier to design complex components in react.

syranide

syranide commented on Oct 1, 2014

@syranide
Contributor

@sebmarkbage I'm not entirely sure exactly what problem contexts are meant to solve. But in the hypothetical context of reinvented styles (without selectors) I can imagine a benefit in being able to propagate hoveringOverTheBigButton (active/etc) to all descendants... and same for implicitly passing props from far up the hierarchy to far down (as is often requested). This should obviously be in the Parent tree then.

It seems that using a GUID style approach would be preferable for avoiding any and all conflicts, components would instead opt-in. Something like:

var Child = React.createClass({
  contextSources: {
    ancestorIsHovered: Parent...hovered
  },
  render: function() {
    return <div>{this.context.ancestorIsHovered ? 'Ancestor hovered' : 'Ancestor not hovered'}</div>;
  }
});

var Parent = React.createClass({
  childContextTypes: {
    hovered: React.PropTypes.boolean
  },
  getChildContext: function() {
    return {hovered: true};
  },
  render: function() {
    return <Child />;
  }
});

But again, I'm not exactly sure what contexts are meant to solve so I could be way way of the mark here.

mjackson

mjackson commented on Oct 16, 2014

@mjackson
Contributor

If someone who is more familiar with the React internals would care to put together a short summary of what needs to be done here, I'd be glad to give it a shot and make a PR. react-router depends heavily on context, and it's starting to break for some people's use cases.

Things I don't fully understand:

  • What exactly is the difference between the parent and the owner trees? Is one DOM-based and the other a React thing?
  • How might this change affect users currently using context?
appsforartists

appsforartists commented on Oct 16, 2014

@appsforartists

I'd love to know more about when/if this might happen and what it will take to get there. I share @1st1's belief that this will make component/library composition better. In my particular case, I'm working on a library that wraps ReactRouter, and I'd like to be able to pass data down from my library, through the routes, and into the app's components. I can't presently do this without either a) changes by ReactRouter to support this directly, or b) the ability to pass a context into an app's already-created routes.

sophiebits

sophiebits commented on Oct 24, 2014

@sophiebits
Collaborator

@sebmarkbage I assume we'll want to support reparenting someday… is there anything we need to keep in mind while doing this? Guessing not but wanted to check.

mjackson

mjackson commented on Oct 25, 2014

@mjackson
Contributor

Things I learned yesterday while talking to @sebmarkbage about this:

  • The parent tree is made up of parent components, not necessarily DOM elements. The owner tree is made up of components that were on the bottom of the render stack at the time a given component was rendered.
  • Changing context to use the parent tree gets us one step closer to being able to remove _owner entirely, which is a good thing.
  • The vast majority of people currently using context should be unaffected by this change since the owner tree is by definition a subset of the parent tree.
sebmarkbage

sebmarkbage commented on Nov 6, 2014

@sebmarkbage
CollaboratorAuthor

62 remaining items

added 2 commits that reference this issue on Apr 7, 2015
a51686b
7d44917
added a commit that references this issue on Apr 9, 2015
0185c68
added a commit that references this issue on Apr 24, 2015
aaeb3d4
added a commit that references this issue on May 28, 2015
217608a
danscan

danscan commented on Jul 20, 2015

@danscan

Does React Native use parent context now as well? Seems like it still uses owner context.

sophiebits

sophiebits commented on Jul 20, 2015

@sophiebits
Collaborator

React Native is still on React 0.13.2. We'll update after the 0.14 final release.

jedwards1211

jedwards1211 commented on Jul 23, 2015

@jedwards1211
Contributor

Cool, glad to know this is on the way! I've also used context to pass down stores, out of a desire to avoid singletons. I've used them in a few other cases as well.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @sophiebits@natew@sebmarkbage@mjackson@jamesknelson

    Issue actions

      Switch the Context to use the Parent tree instead of the Owner tree · Issue #2112 · facebook/react