因此,我尝试在react的div属性中检索数据。JSFiddle显示了我遇到的问题。事件激发后,this.state.value被设置为空字符串。
在读取another stack overflow post之后,我开始使用div的data-*属性,但我不能使用jQuery,所以我切换到id。有什么想法吗?
class GettingAttributes extends React.Component {
constructor(props) {
super(props)
this.state = {value: 'Hello!'};
this.bar = this.bar.bind(this);
}
bar(e){
this.setState({value: e.target.id})
}
render() {
return (
<div id="foo" onClick={this.bar}>
<p>{this.state.value}</p>
</div>
);
}
}
https://stackoverflow.com/questions/50956462
复制相似问题