Alex Povar Alex Povar - 3 months ago 13
React JSX Question

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

Is it safe to pass

undefined
in React's
onClick
handler?

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

Example:

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

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.

Comments