[英]Understanding how to refer to properties in react components
I can not get this basic component to recognize the this
object, whatever I try I get the error: 我不能让这个基本组件识别this
对象,无论我尝试什么,我得到错误:
Uncaught TypeError: Cannot read property 'getEvents' of undefined 未捕获的TypeError:无法读取未定义的属性'getEvents'
This is the code im trying to get working: 这是我试图开始工作的代码:
import React from 'react'
import PureRenderMixin from 'react-addons-pure-render-mixin';
import { connect } from 'react-redux';
import ListGroup from 'react-bootstrap/lib/ListGroup';
import ListGroupItem from 'react-bootstrap/lib/ListGroupItem';
export const Selector1 = React.createClass({
mixins: [PureRenderMixin],
getEvents: () => this.props.eventTypes || [{name: "NO EVENTS!"}],
render: _ =>
<ListGroup>
{this.getEvents().map(event =>
<ListGroupItem>{event.name}</ListGroupItem>
)}
</ListGroup>
});
function mapStateToProps(state) {
return {
eventTypes: state.get('eventTypes')
};
}
export const Selector1Container = connect(mapStateToProps)(Selector1);
Why is this
undefined at runtime and what do I need to do for this to actually refer to what I want? 为什么是this
不确定的在运行时需要什么该做,实际上是指我想要什么?
Change arrow function
( =>
) to normal function because arrow function does not have own this
将arrow function
( =>
)更改为正常函数,因为箭头函数没有this
函数
export const Selector1 = React.createClass({
mixins: [PureRenderMixin],
getEvents() {
return this.props.eventTypes || [{name: "NO EVENTS!"}]
},
render() {
return <ListGroup>
{this.getEvents().map(event =>
<ListGroupItem>{event.name}</ListGroupItem>
)}
</ListGroup>
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.