Providing default value for prop function in React

What is the best way to handle additional React.PropType.func properties?

Should I provide a default noop for it (if this is the best way) or should I just check if support is defined?

 propTypes: { onClick: React.PropTypes.func }, someMethod: function() { if (this.props.onClick) { this.props.onClick(); } } 
+6
source share
2 answers

React has getDefaultProps . Then you can call onClick without errors.

 getDefaultProps: function() { return { onClick: function() {} }; } 
+2
source

You can do with typeof :

 if (typeof this.props.onClick === 'function'){ //... } 
0
source

All Articles