question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warning `Unsafe legacy lifecycles will not be called for components ...` keeps appearing

See original GitHub issue

Do you want to request a feature or report a bug? Might be a bug?

What is the current behavior? Replaced componentWillReceiveProps() with a newly added getDerivedStateFromProps() but the warning keeps appearing in the console.

I doubted the higher order component at first sight, so removed all the coupling with this component’s higher order wrapper. I also removed children in render() in an effort to remove all the side effects.

Apparently there’s no componentWillReceiveProps() in this component but I cannot get rid of this warning. The only thing I could do was to explicitly assign null to this.componentWillReceiveProps, then the warning was gone.

Am I missing something or is it a bug? I also wish this warning could be optionally turned off in development environment. (Considering the legacy React libraries still use them and can be injected by e.g. HOC)

image

class RootContainer extends React.Component {
  constructor() {
    super();
    this.state = {
    };
    this.componentWillReceiveProps = null; // Only way I can turn that warning off
  }

  static getDerivedStateFromProps(nextProps, prevState) {
    // ...
    return prevState;
  }

  render() {
    return (
      <div>1</div>
      // <Root>
      //   {this.props.history 
      //     ? this.props.children 
      //     : null}
      // </Root>
    );
  }
}

// ...

export default RootContainer;
// export default withRouter(connect(makeMapStateToProps)(RootContainer));

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

What is the expected behavior? No warning (or at least to turn that off by option even in development environment)

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

  • React 16.3.0
  • React-DOM 16.3.0
  • React-Redux 5.0.7
  • React-Router-DOM 4.2.2

UPDATE Apr 4

  • React-hot-loader 4.0.0 (CAUSE OF WARNING)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Apr 4, 2018

@OleksandrBudik Make sure you use the latest react-redux (there are updates available both for 4.x and 5.c branches). Most likely you’re using an older version that copies all static methods (including the new lifecycle) to the wrapping component, causing this warning. The newer version skips this lifecycle.

0reactions
allexysleepscommented, Oct 29, 2018

I have the same issue. It also causes serious bug alongside with warning. It fires getDerivedState… every time component updates.

It seems to be a react-redux issue. I’ve updated to the 5.1.0 Didn’t help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unsafe legacy lifecycles will not be called for components ...
I get this error with React v16.3: warning.js:33 Warning: Unsafe legacy lifecycles will not be called for components using new component APIs.
Read more >
Unsafe legacy lifecycle methods should not ... - Rule | DeepScan
In React v16.3, componentWillMount() , componentWillReceiveProps() , and componentWillUpdate() are marked as unsafe legacy lifecycle methods ...
Read more >
What is StrictMode in React? - KnowledgeHut
Detecting dangerous lifecycle methods that React has deprecated. Unexpected side effects in React components are detected. What Does Strict Mode ...
Read more >
Strict Mode - React
StrictMode currently helps with: Identifying components with unsafe lifecycles; Warning about legacy string ref API usage; Warning about deprecated findDOMNode ...
Read more >
React Components rendered twice — any way to fix this?
Many developer have implemented a similar functional component and have seen this ... Identifying components with unsafe lifecycles · Warning about legacy ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found