admin管理员组文章数量:1332377
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
};
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
};
Share
Improve this question
asked Aug 28, 2016 at 17:44
Alex PovarAlex Povar
4,9603 gold badges32 silver badges44 bronze badges
2 Answers
Reset to default 8It'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.
passing undefined
can be handled by checking it on your onClick
,
function MonthBar(props) {
/* is it okay when props.onClick === undefined ? */
return (
<span onClick={props.onClick !== undefined? props.onClick : ''}>
{props.monthName}
</span>);
}
MonthBar.propType = {
onClick: React.PropTypes.func, // optional
monthName: React.PropTypes.string.isRequired
};
the concern should be more about you want to pass the undefined
to the onClick or not
本文标签: javascriptIs it safe to pass undefined in React39s onClickStack Overflow
版权声明:本文标题:javascript - Is it safe to pass undefined in React's onClick? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742305661a2449866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论