我试图在来自 General 类的函数 Application 中获取“状态”对象,但收到此错误“未捕获的类型错误:无法读取未定义的属性“状态””。
代码是
class General extends Comment {
constructor() {
super();
this.state = { comments: first_comment};
}
}
const Application = () => {
return (
<div> Hello world beginner: {this.state.comments}</div>
);
};
render(<Application/>, document.getElementById('container'));
应用程序是无状态组件。并不是说箭头函数具有上下文的词法作用域。
对无状态组件使用 props。
const Application = (props) => {
return (
<div> Hello world beginner: {props.comments}</div>
);
};
或者扩展 React.Component
class Application extends React.Component {
constructor() {
// init state
}
render() {
return <div> Hello world beginner: {this.state.comments}</div>
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)