我使用React(在Rails中使用react_on_rails gem)并且我收到错误消息:
TypeError:_this2.props.people未定义
import React from 'react';
var people = [
{
"name": "Sister Altenwerth",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/ryandownie/128.jpg",
"id": 3
}
]
class Card extends React.Component {
render() {
return (
<div>
<h2>{this.props.name}</h2>
<img src={this.props.avatar} alt=""/>
<div></div>
<button onClick={this.props.onClick}>Delete Me</button>
</div>
)
}
}
class Foo extends React.Component {
deletePerson(person) {
this.state.people.splice(this.state.people.indexOf(person), 1);
this.setState({people: this.state.people});
}
constructor(props) {
super(props);
this.state = {
people: this.props.people.splice(0)
}
}
render() {
var that = this;
return (
<div>
{this.state.people.map(function (person) {
return (
<Card key={person.id} onClick={that.deletePerson.bind(null, person)} name={person.name} avatar={person.avatar}></Card>
)
}, this)}
</div>
)
}
}
export default Foo
我已经完成了反应文档,我仍然无法弄清楚代码问题在哪里。也许它与react_on_rails gem有关。
原始代码使用React.createClass()函数并将其转换为使用ES6类扩展语法和构造函数而不是getInitialState()。