I have an optional propType
static defaultProps = {
onSort: undefined, // undefined | () => {}
}
If undefined, we will check for undefined
before the function is called.
_handleSort = () => {
this.props.onSort && this.props.onSort()
}
So, how do I handle the propType of type Function | undefined
. Should I check for undefined before calling the function or define a default function () => {}
This issue suggests defining a default function as you have indicated above: https://github.com/yannickcr/eslint-plugin-react/issues/1067
Cmp.defaultProps = {
onChange: () => {}
}
However, I am still trying to get this to work. I'd be interested if you have any luck.
static propTypes = {
onSort: PropTypes.oneOfType([
PropTypes.func,
PropTypes.any
])
}
Then check if onSort
exist.
if (this.props.onSort) {
//do something
}
OR
Define the propTypes
like this:
static propTypes = {
onSort: PropTypes.func
}
then have a default for onSort
like so:
static defaultProps = {
onSort: () => null
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With