我想知道,如何設置我的狀態使用ES6如何設置狀態與react.js ES6道具
道具參數前:
const MyComponent = React.createClass({
getInitialState: function() {
return {
name: this.props.name || 'Initial data...'
};
}
});
現在我努力做到這一點,this.props
不存在:
class MyComponent extends React.Component {
constructor() {
super()
this.state = { name: this.props.name || 'Joseph Flowers' };
}
}
你好@jail我試圖實現你的解決方案,但是跳出一個錯誤:static defaultProps = {'> 4 |靜態defaultProps = { |^ 5 | \t名稱:'默認名稱...' 6 | };'node - version - > v6.3.1 –
@Jail,你爲什麼使用React.PureComponent? –
對不起@Jail其他問題...我如何驗證props.name是否不是空字符串?導致其他componente發送和空字符串,但在這種情況下,我想設置默認值爲例如this.props.name ===''? ''this.props.name:'默認值' –