单击DOM元素时,未定义e.key; 我如何理解DOM的关键字?
constructor(props) {
super(props);
this.__handle = this.__handle.bind(this);
}
__handle(e){
console.log(e);
console.log(e.key);
}
render() {
let that = this;
let data = [1,2,3,4];
let com = data.map(function(data,index){
return <div key={index} onClick={that.__handle}>data</div>
})
return <div>
{com}
</div>;
}
React的版本是^ 15.6.1。
谢谢!
答案 0 :(得分:0)
我以为你有这种疑问。
import React from 'react';
class App extends React.Component{
constructor(props){
super(props);
this.handleClick = this.handleClick(this);
}
handleClick(data){
console.log(data);
}
render(){
let data = [1,2,3,4];
return data.map(val=>{
return <div onClick={this.handleClick.bind(this, val)}>{val}</div>
})
}
}
我希望这对你有用!