反应:未捕获的TypeError:无法读取未定义的属性“状态" [英] react: uncaught TypeError: Cannot read property 'state' of undefined
本文介绍了反应:未捕获的TypeError:无法读取未定义的属性“状态"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从通用类中获取功能应用程序中的状态"对象,并且收到此错误未捕获的TypeError:无法读取未定义的属性"状态".代码是
I am trying to get 'state' object in function Application which is out from General class and I am getting this error "Uncaught TypeError: Cannot read property 'state' of undefined". The code is
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'));
推荐答案
应用程序是无状态组件.更不用说箭头函数具有上下文的词汇范围.
Application is stateless component. Not to say that arrow functions have lexical scoping of context.
将道具用于无状态组件.
Use props for stateless components.
const Application = (props) => {
return (
<div> Hello world beginner: {props.comments}</div>
);
};
或扩展React.Component
Or extend React.Component
class Application extends React.Component {
constructor() {
// init state
}
render() {
return <div> Hello world beginner: {this.state.comments}</div>
}
}
这篇关于反应:未捕获的TypeError:无法读取未定义的属性“状态"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文