Eslint-plugin-react: What is the best way of defining optional event handlers in propTypes?

Created on 9 Feb 2017  路  2Comments  路  Source: yannickcr/eslint-plugin-react

I have a component that has an onChange event. Providing the event handler for this component is optional. I have defined the propTypes as follows:

Cmp.propTypes = {
  onChange: React.PropTypes.func
}

But I will get the react/require-default-props eslint error.
My question is that how can I define a propType of Function | undefined?

question

Most helpful comment

You can also set null or undefined as the default, since all non-required props allow those.

All 2 comments

The rule is checking if there is a default prop for non required props.

You can set a noop function as the default:

Cmp.defaultProps = {
    onChange: () => {}
}

You can also set null or undefined as the default, since all non-required props allow those.

Was this page helpful?
0 / 5 - 0 ratings