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

Blocks context propagation #35

Open
benvinegar opened this issue Oct 30, 2016 · 3 comments
Open

Blocks context propagation #35

benvinegar opened this issue Oct 30, 2016 · 3 comments

Comments

@benvinegar
Copy link

benvinegar commented Oct 30, 2016

The behavior is caused by react-side-effect here (shouldComponentUpdate only does a shallow compare on props, see facebook/react#2517), but the upshot is that using <DocumentTitle/> stops context propagation for a context type declared in its parent but consumed by a descendent.

Totally untested-illustrative-example:

const Parent = React.createClass({
  childContextTypes: {
    foo: React.PropTypes.any
  },
  getChildContext() {
    return { foo: this.state.foo };
  },
  getInitialState() {
    return { foo: null };
  },
  componentDidMount() {
    setTimeout(() => this.setState({foo: 'bar'});
  },
  render() {
    return <DocumentTitle title="sup"><Child/></DocumentTitle/>
  }
});

const Child = React.createClass({
  contextTypes: {
    foo: React.PropTypes.string
  },
  render() {
    // never gets re-rendered with updated `foo` context value
    return <div>{this.context.foo}</div>;
  }
});

I realize that facebook/react#2517 has been discussed to death / is documented clearly in the React Context documentation, but I didn't expect to encounter it in react-document-title (I did not find it in the README).

@benvinegar
Copy link
Author

benvinegar commented Nov 18, 2016

On the off-chance anyone discovers this, my solution was to call forceUpdate directly on the <DocumentTitle/> component from Parent in situations where I knew <DocumentTitle/> would not re-render (context changes).

Implemented here: https://github.com/getsentry/sentry/pull/4559/files

@victor-homyakov
Copy link

Any progress on fixing this?

@Kepro
Copy link

Kepro commented Aug 24, 2018

it's not a problem with this library, it's how React works... also this context API is deprecated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants