Alex Povar Alex Povar - 1 year ago 101
React JSX Question

Is it safe to pass undefined in React's onClick?

Is it safe to pass

in React's

I just tried and everything still works, but I can't find any line about that in docs.


function MonthBar(props) {
/* is it okay when props.onClick === undefined ? */
return <span onClick={props.onClick}>{props.monthName}</span>;

MonthBar.propType = {
onClick: React.PropTypes.func, // optional
monthName: React.PropTypes.string.isRequired

Answer Source

It's perfectly valid to pass in undefined as the onClick handler. They already have to handle that case implicitly since it's an optional parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download