React-apollo: Warning: Apollo(xxx): Did not properly initialize state during construction. Expected state to be an object, but it was undefined.

Created on 5 Apr 2018  路  6Comments  路  Source: apollographql/react-apollo

I am upgrading my project to react 16.3.1 but in components with HOC generated with react-apollo I am getting this errors and my app crashed.

"Apollo(SubmittedFormsList): Did not properly initialize state during construction. Expected state to be an object, but it was undefined."

this is the code I am expecting to work (Recommended By React):

Error: "TypeError: prevState is null"

static getDerivedStateFromProps(nextProps, prevState) {
    if(nextProps.showModal !== prevState.showModal) {
      console.log('recived new prop');
      return { showModal: true };
    } else {
      return null
    }
  };

and this is the code actually works for me (NOT Recommended By React):

componentWillReceiveProps(nextProps) {
    if(nextProps.showModal !== this.state.showModal) {
      console.log('recived new prop');
      this.setState({ showModal: true }) ;
    };
  };

Most helpful comment

We are having the exact same issue, and I think it is due to the fact that react apollo is using an outdated hoist-non-react-statics that doesn't support React 16.3, but I'm not 100% sure on that... In our case the app doesn't crash, but just throws that warning in the console. Im going to debug it a bit more and if I find the cause, I'll report back!

All 6 comments

We are having the exact same issue, and I think it is due to the fact that react apollo is using an outdated hoist-non-react-statics that doesn't support React 16.3, but I'm not 100% sure on that... In our case the app doesn't crash, but just throws that warning in the console. Im going to debug it a bit more and if I find the cause, I'll report back!

same issue here

This should normally be fixed. Be sure you have te latest version of react-apollo by running yarn upgrade react-apollo.

Seeing same warning here.

This issue should be closed now.

Closing - housekeeping

Was this page helpful?
0 / 5 - 0 ratings

Related issues

voodooattack picture voodooattack  路  3Comments

reggi picture reggi  路  3Comments

notadamking picture notadamking  路  3Comments

bdouram picture bdouram  路  3Comments

jakelacey2012 picture jakelacey2012  路  3Comments