`react/no-unused-state` warning in new `getDerivedStateFromProps` methods
See original GitHub issueeslint 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:
- Created 5 years ago
- Reactions:27
- Comments:23 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think if the method name is
getDerivedStateFromProps
on the component, then the prop names should be irrelevant?Actually the rule did not detected the usage since you did not used the standard name for the
getDerivedStateFromProps
arguments (it should benextProps
andprevState
instead ofprops
andstate
).I don’t know if this is fixable without getting a lot of false positive.