ifステートメントを使用するよりも条件付きで小道具を渡すためのより良い方法があるかどうか知りたいです。
たとえば、今私は持っています:
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}
/>
);
}
});
まとめ:の小道具を定義する方法を見つけようとしていますが、その小道具の値を自分自身からプルするChildような値を渡します(または何か他のことをします)。ChildChildgetDefaultProps()
Childか?また、<Child editableOpts={this.props.editableOpts} />代わりに言うつもりでした<Child editable={this.props.editableOpts} />か?