मैं यह जानना चाहूंगा कि क्या इफ-स्टेटमेंट का उपयोग करने की तुलना में किसी प्रस्ताव को सशर्त रूप से पारित करने का एक बेहतर तरीका है।
उदाहरण के लिए, अभी मेरे पास है:
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 />
);
}
}
});
क्या इफ-स्टेटमेंट के बिना इसे लिखने का कोई तरीका है? मैं JSX में एक प्रकार की इनलाइन-इफ-स्टेटमेंट की तर्ज पर कुछ सोच रहा था:
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, लेकिन एक मान (या कुछ और करना) ऐसा है जो Childअभी भी उस प्रोप के मूल्य को Childखुद से खींचता है getDefaultProps()।
Childको भी शामिल कर सकते हैं ? इसके अलावा, आप के<Child editableOpts={this.props.editableOpts} />बजाय कहने का मतलब था<Child editable={this.props.editableOpts} />?