Eslint-plugin-react: Destructuring values from this.<anything> in componentDidUpdate causes prop-types errors

Created on 16 Oct 2018  Â·  3Comments  Â·  Source: yannickcr/eslint-plugin-react

Note: This only happens with master, not with the latest stable release.

error 'state.bar' is missing in props validation react/prop-types

Example code:

class Test extends React.Component {
    componentDidUpdate() {
        const {bar} = this.state;
        console.log(bar);
    }

    render() {
        return null;
    }
}
$ npx eslint test.js

/home/adrian/dev/indico/src/test.js
  1:1   error  This module could be parsed as a valid script  import/unambiguous
  1:7   error  'Test' is defined but never used               no-unused-vars
  1:20  error  'React' is not defined                         no-undef
  3:16  error  'state.bar' is missing in props validation     react/prop-types

✖ 4 problems (4 errors, 0 warnings)

Disregard the first three errors; I just couldn't be bothered to add the imports etc to the test file - the 4th error is not related to those.

bug

Most helpful comment

Oh 😞 I'll take a look

All 3 comments

Looking at the code this happens in any lifecycle method, regardless of what this. attribute is destructured. I guess the check in isPropTypesUsage is too broad since it doesn't look at the name of the attribute that's destructured in case of a lifecycle method.

cc @alexzherdev

Oh 😞 I'll take a look

Was this page helpful?
0 / 5 - 0 ratings