What is the best way to handle optional React.PropType.func
properties?
Should I provide default noop for it (if so what's the best way) or should I just check if the prop is defined?
propTypes: {
onClick: React.PropTypes.func
},
someMethod: function() {
if (this.props.onClick) {
this.props.onClick();
}
}
i am using anonymous empty arrow function... correct me if this is stupid :)
component.defaultProps = {
foo: () => {},
};
React has getDefaultProps. Then you can call onClick without errors.
getDefaultProps: function() {
return {
onClick: function() {}
};
}
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