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

asynchronous context change is not propagated to the connected components #311

Closed
zdila opened this issue Mar 5, 2016 · 1 comment
Closed

Comments

@zdila
Copy link

zdila commented Mar 5, 2016

Please see the following example. The only difference between Bar and ConnectedBaz is that the second one uses redux connect.

The problem is that the context changes do not get propagated to the component Baz and it statically renders 0. Cmponent Bar is being updated and re-rendered correctly every second.

import React, {PropTypes} from 'react';
import { connect } from 'react-redux';

class Bar extends React.Component {
  static contextTypes = {
    time: React.PropTypes.number
  };

  render() {
    return <div>{this.context.time}</div>;
  }
}

class Baz extends React.Component {
  static contextTypes = {
    time: React.PropTypes.number
  };

  render() {
    return <div>{this.context.time}</div>;
  }
}

const ConnectedBaz = connect()(Baz);

export default class Foo extends React.Component {
  static childContextTypes = {
    time: React.PropTypes.number
  };

  getChildContext() {
    return {
      time: this.state ? this.state.time : 0
    };
  }

  componentDidMount() {
    setInterval(() => {
      this.setState({time: new Date().getTime()});
    }, 1000);
  }

  render() {
    return <div><Bar/><ConnectedBaz/></div>;
  }
}
@gaearon
Copy link
Contributor

gaearon commented Mar 5, 2016

Yes, this is a known React issue.
There are a few possible fixes:

  • Don’t rely on context updates propagating correctly and instead expose something like subscribeToMyThing() in context. (This is pretty much what React Redux does by the way.)
  • Pass { pure: false } as the fourth argument to connect(). This will significantly slow it down but context changes will propagate.
  • Wait for React to fix this or help them to 😉

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

2 participants