4m1*_*m1r 10 ecmascript-6 reactjs
我正在尝试使用对象文字设置defaultProp,但过了一段时间后我意识到React类构造函数没有将默认道具与应用的道具合并,所以我最终得到了defaultProps文字中任何属性的未定义值没有被包括在应用道具中.有没有办法合并默认道具和应用道具,还是我需要将我的对象分解成几个道具?
class Test extends React.Component {
constructor(props) {
super(props);
//props.test is only {one: false}
//props.test.two is undefined
}
render() {
return (<div>render</div>)
}
}
Test.defaultProps = {
test: {
one: true,
two: true
}
}
ReactDOM.render(<Test test={{'one': false}}/>, document.getElementById('#test'));
Run Code Online (Sandbox Code Playgroud)