Skip to content

Spurious context warning when getChildContext isn't pure #3709

@sophiebits

Description

@sophiebits

@sahrens found a case where we had a component that was creating a new object each time getChildContext was called. This isn't optimal for performance because getChildContext is called for each render, but it looks like we're actually calling it twice for each render and warning if the two results aren't the same. This code

var Parent = React.createClass({
  childContextTypes: {
    x: React.PropTypes.number
  },
  getChildContext: function() {
    return {x: Math.random()};
  },
  render: function() {
    return <Child />;
  }
});

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

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

produces warnings like

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions