Destructured use of property is not recognized by no-unused-prop-types
See original GitHub issueGiven a React component like:
export default class Thing extends React.Component {
static propTypes = {
i18n: PropTypes.shape({
gettext: PropTypes.func,
}).isRequired,
}
render() {
const { i18n } = this.props;
return (
<div>
<span>{i18n.gettext('Some Text')}</span>
</div>
);
}
}
And the following eslint rule:
"react/no-unused-prop-types": "error"
I see the following error:
/Users/kumar/tmp/eslint-scratch/index.js
6:16 error 'i18n.gettext' PropType is defined but prop is never used react/no-unused-prop-types
This is incorrect because the i18n
property is destructured into a new constant and then the gettext()
function is called. If I edit the code so that it doesn’t use destructuring then the error goes away, revealing the bug.
Here is a small app that reproduces it: eslint-scratch.zip. Run:
npm install
npm run eslint
This is similar to https://github.com/yannickcr/eslint-plugin-react/issues/782 but they seem different.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:48
- Comments:37 (14 by maintainers)
Top Results From Across the Web
Destructured use of property is not recognized by no-unused ...
Given a React component like: export default class Thing extends React.Component { static propTypes = { i18n: PropTypes.shape({ gettext: ...
Read more >Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from ...
Read more >When I try to use variable that is result of object destructuring ...
The reason is because in TS types are not sealed, meaning they express required fields but don't disallow excess properties.
Read more >Prevent Error with Default {} when Destructuring
When you use destructuring, make sure to set a default value of empty {} to prevent it from throwing an error! function hi(person)...
Read more >Object destructuring best practice in Javascript | by Crunch Tech
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from ...
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 am seeing this as well when using
PropTypes.arrayOf(PropTypes.shape({...})
and then mapping the prop inrender
I have a warning with flow too. My code:
And my solution:
I can create a repro if it’s necessary.