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?
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.
Most helpful comment
You can also set
nullorundefinedas the default, since all non-required props allow those.