Skip to content

Wrong context warning? - 'Warning: owner-based and parent-based contexts differ' #3451

Closed
@davidagraf

Description

@davidagraf

IMO: the warning ...

Warning: owner-based and parent-based contexts differ (values: `context value` vs `undefined`) for key (foo) while mounting Child (see: http://fb.me/react-context-by-parent)

... in the following code is wrong.

In the code, I define a root component with a context value. The root component show two different children dependent on a state variable. When the root switched from one child to another, the warning is shown.

Example on jsFiddle: http://jsfiddle.net/davidagraf/548u0o0a/

Run tested with React v0.13.1.

var Root = React.createClass({
  childContextTypes: {
    foo: React.PropTypes.string
  },
  getChildContext: function() {
    return {
        foo: "context value"
    };
  },
  getInitialState: function () {
    return {foo: false};
  },
  render: function() {
    if (this.state.foo) {
      return <Child/>;
    } else {
      setTimeout(function() { this.setState({foo: true}); }.bind(this), 1000);
      //return <Child/>;
      return <div>loading</div>;
    }
  }
});

var Child = React.createClass({
  contextTypes: {
    foo: React.PropTypes.string
  },
  render: function() {
    return <div>{this.context.foo}</div>;
  }
});

React.render(<Root/>, document.body);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions