我正在尝试从通用类中获取功能应用程序中的“状态”对象,并且收到此错误“未捕获的TypeError:无法读取未定义的属性”状态”。该代码是
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'));
应用程序是无状态组件。并不是说箭头功能具有上下文的词汇范围。
对无状态组件使用道具。
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> } }