我最近开始使用反应,我倾向于这样定义的默认值:的defaultProps VS逻辑或
class TextInput extends Component {
render() {
return (
<input
type="text"
name={ this.props.inputName || 'inputName' }
style={ this.props.inputStyle || {} }
className={ this.props.inputClass || '' }
/>
);
}
}
代替:
class TextInput extends Component {
render() {
return (
<input
type="text"
name={ this.props.inputName}
style={ this.props.inputStyle}
className={ this.props.inputClass}
/>
);
}
}
TextInput.defaultProps = {
inputName: 'inputName',
inputStyle: {},
inputClass: ''
}
没有这种方法有对比有什么缺点使用defaultProps
?
我明白了,谢谢你的回答 – sleepwalker00