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.

`react/no-unused-state` warning in new `getDerivedStateFromProps` methods

See original GitHub issue

eslint versions

"eslint": "4.19.1",
"eslint-config-airbnb": "16.1.0",
"eslint-loader": "2.0.0",
"eslint-plugin-import": "2.10.0",
"eslint-plugin-jsx-a11y": "6.0.3",
"eslint-plugin-mocha": "5.0.0",
"eslint-plugin-react": "7.7.0",
"extract-text-webpack-plugin": "3.0.2",

If the only place you reference state is inside getDerivedStateFromProps then the eslint warning triggers for no-unused-state. See the example below:

import React, { Component } from 'react';


export default class ESLintExample extends Component {
  static getDerivedStateFromProps(nextProps, prevState) {
    if (prevState.id === nextProps.id) {
      return {
        selected: true,
      };
    }
    return null;
  }
  constructor(props) {
    super(props);
    this.state = {
      // This is giving a warning even though it is used inside
      // getDerivedStateFromProps
      id: 123,
    };
  }

  render() {
    return (
      <h1>{this.state.selected ? 'Selected' : 'Not selected'}</h1>
    );
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:27
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

10reactions
ljharbcommented, May 15, 2018

I think if the method name is getDerivedStateFromProps on the component, then the prop names should be irrelevant?

7reactions
yannickcrcommented, May 15, 2018

Actually the rule did not detected the usage since you did not used the standard name for the getDerivedStateFromProps arguments (it should be nextProps and prevState instead of props and state).

I don’t know if this is fixable without getting a lot of false positive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React.js static getDerivedStateFromProps() - GeeksforGeeks
The getDerivedStateFromProps() method is used when the state of a component depends on changes of props. getDerivedStateFromProps(props ...
Read more >
Why use getDerivedStateFromProps when you have ...
A static method is a method / function that exists on the class not its instance. ... and you will see a warning...
Read more >
You Probably Don't Need Derived State – React Blog
The getDerivedStateFromProps bugfix in 16.4 makes derived state more predictable, so the results of misusing it are easier to notice.
Read more >
Replacing 'componentWillReceiveProps' with ... - Medium
Notice an object is being returned in the getDerivedStateFromProps to update the state and no class method is being called. We're using componentDidUpdate...
Read more >
Using Derived State in React - DigitalOcean
... the new getDerivedStateFromProps method in your React components. ... Notice that the function has no side-effects; this is intentional.
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