我想知道是否有比使用if语句更好的方法来有条件地传递prop.
例如,现在我有:
var parent = React.createClass({
propTypes: {
editable: React.PropTypes.bool.isRequired,
editableOpts: React.PropTypes.shape({...})
},
render: function() {
if(this.props.editable) {
return (
<Child editable={this.props.editableOpts} />
);
} else {
// In this case, Child will use the editableOpts from its own getDefaultProps()
return (
<Child />
);
}
}
});
有没有一种方法可以不用if语句来写呢?我在考虑JSX中的一种内联if语句:
var parent = React.createClass({
propTypes: {
editable: React.PropTypes.bool.isRequired,
editableOpts: React.PropTypes.shape({...})
},
render: function() {
return (
<Child
{this.props.editable ? editable={this.props.editableOpts} : null}
/>
);
}
});
To wrap-up:我试图找到一种方法来定义Child
的props ,但传递一个值(或做其他事情),这样Child
仍然会从Child
自己的getDefaultProps()
中提取props 的值.