ReactJS-传递函数
你们知道为什么,当我调用map之外的函数时,它可以工作,但不能在map内部工作吗? 我试图在map的回调函数参数中传递“this”。我尝试在地图内部内联绑定,但什么都没用ReactJS-传递函数,reactjs,dictionary,Reactjs,Dictionary,你们知道为什么,当我调用map之外的函数时,它可以工作,但不能在map内部工作吗? 我试图在map的回调函数参数中传递“this”。我尝试在地图内部内联绑定,但什么都没用 var React = require('react'); var PropTypes = require('prop-types'); class List extends React.Component { constructor(props) { super(props); this.state =
var React = require('react');
var PropTypes = require('prop-types');
class List extends React.Component {
constructor(props) {
super(props);
this.state = {
events: props.events
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(e){
console.log("Boom!")
}
render() {
return (
<div>
<table onClick={this.handleClick}><tbody>
{this.props.events.map(function(event){
return(
<tr key={event.position}>
<td onClick={this.handleClick}>{event.position}</td>
<td>{event.start_time}</td>
<td>{event.duration}</td>
<td>{event.end_time}</td>
<td>{event.text}</td>
</tr>
)
})}
</tbody></table>
</div>
)
}
}
module.exports = List;
var React=require('React');
var PropTypes=require('prop-types');
类列表扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
事件:道具
};
this.handleClick=this.handleClick.bind(this);
}
handleClick(e){
console.log(“Boom!”)
}
render(){
返回(
{this.props.events.map(函数(事件)){
返回(
{event.position}
{event.start_time}
{event.duration}
{event.end_time}
{event.text}
)
})}
)
}
}
module.exports=列表;
请建议将其更改为胖箭头函数,使
此
返回到外部范围。函数
以您使用的方式执行时,会创建一个新的此
{this.props.events.map((event) => {
...
})}
非常感谢,这很有帮助。即使在构造函数中没有绑定,它也可以工作。我听说胖箭头函数的工作方式不同。不使用它是否可以达到同样的效果?我试图理解为什么这次绑定不起作用。有一天,我试图变得聪明:啊,这看起来比我试图做的事情更有意义。谢谢老板!