我需要获取一个我已经克服的组件并查看其目标属性。我试图得到它但是evt param是未定义的
getcomponent(evt){
console.log(evt.target)
//...
}
//...
render() {
return (<button id="btn" onClick={() =>this.getcomponent()}></button>);
}
答案 0 :(得分:1)
将event
作为参数添加到onClick
:
render() {
return (<button id="btn" onClick={(event) =>this.getcomponent(event)}></button>);
}
答案 1 :(得分:1)
您没有将事件传递给函数调用。传递这样的事件:
onClick={(evt) => this.getcomponent(evt)}
。
答案 2 :(得分:1)
让代码简洁明了:
onClick = event => {
console.log(event.target)
}
render() {
return <button id="btn" onClick={this.onClick}></button>
}
答案 3 :(得分:1)
您需要传递事件才能将其取回。这是代码。
class TestJS extends React.Component {
constructor(props) {
super(props);
this.getcomponent = this.getcomponent.bind(this);
}
getcomponent(event){
console.log(event.target);
}
render() {
return(
<div id="root">
<button id="btn" onClick={(event) =>this.getcomponent(event)}></button>;
</div>
)};
}
export default TestJS;
&#13;