Skip to content

Connected components cannot use new 16.3 lifecycles #915

Closed
@byronanderson

Description

@byronanderson

I know that there is still much consideration going into #890 so I don't mean to rush, but I thought you might be interested to know:

It looks like React 16.3 exposes the new lifecycles in a way that disallow for using the old componentWill* at the same time as new lifecycles.

Something that looks roughly like this:

class Form extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {initialized: false};
  }
  static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.data && !prevState.initialized) {
      return {initialized: true};
    }
    return null;
  }
  render() {
    return this.state.initialized ? "initialized" : "uninitialized"
  }
}
export default connect()(Form);

will get this warning:

screen shot 2018-03-30 at 9 24 26 am

which I didn't understand until I saw that the implementation details of connect use inheritance to add those lifecycles.

My workaround in this first case I found in my code is pretty easy to fix (I was just using the connect to get a dispatch method) but I bet people are going to start running into this as they upgrade to 16.3.

Sending my best to your efforts in #890 !

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