ndb*_*ent 32 javascript reactjs
我正在使用Babel的ES6课程.我有一个看起来像这样的React组件:
import { Component } from 'react';
export default class MyReactComponent extends Component {
getInitialState() {
return {
foo: true,
bar: 'no'
};
}
render() {
return (
<div className="theFoo">
<span>{this.state.bar}</span>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
它看起来不像getInitialState是被调用,因为我收到了这个错误:Cannot read property 'bar' of null.
Des*_*ley 31
代码与Nathans一起回答:
import { Component } from 'react';
export default class MyReactComponent extends Component {
constructor(props) {
super(props);
this.state = {
foo: true,
bar: 'no'
};
}
render() {
return (
<div className="theFoo">
<span>{this.state.bar}</span>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10718 次 |
| 最近记录: |