Eslint-plugin-react: react/prop-types not working in getDerivedStateFromProps

Created on 2 Jul 2018  路  3Comments  路  Source: yannickcr/eslint-plugin-react

When a property is used in getDerivedStateFromProps but not defined in defaultProps this doesn't give me a warning or error.

In the below case baz gives me a react/prop-types error, I expect this same error from bar.

class TestClass extends React.PureComponent {
    static getDerivedStateFromProps(props) {
        const { foo, bar } = props;
        return {
            foobar: foo(bar),
        };
    }

    render() {
        const { baz } = this.props;
        const { foobar } = this.state;

        return <div>{foobar}</div>;
    }
}

TestClass.defaultProps = {
    foo: PropTypes.func.isRequired,
};
bug help wanted react 16

Most helpful comment

It's set to 16.4, so that should not be the issue. It does complain about non "UNSAFE_" legacy lifecycle methods.

All 3 comments

What React version is specified in your eslint config's "settings"? If it's not 16.3+, then the plugin won't know it's a lifecycle method.

It's set to 16.4, so that should not be the issue. It does complain about non "UNSAFE_" legacy lifecycle methods.

Perhaps that example is meant to say not defaultProps but propTypes?

Was this page helpful?
0 / 5 - 0 ratings