Reactjs 设置ES6中的状态

Reactjs 设置ES6中的状态,reactjs,es6-class,Reactjs,Es6 Class,我只是在学习React&我似乎无法让componentdidmount函数中的setstate正常工作。如果你能帮我,那就太可爱了。我已经试过把它绑起来了 我不断收到错误,例如无法读取未定义的属性“setState” 类ShareEvent扩展了React.Component{ 建造师(道具){ 超级(道具); this.state={copied:false}; this.componentDidMount=this.componentDidMount.bind(this); } compo

我只是在学习React&我似乎无法让componentdidmount函数中的setstate正常工作。如果你能帮我,那就太可爱了。我已经试过把它绑起来了

我不断收到错误,例如无法读取未定义的属性“setState”

类ShareEvent扩展了React.Component{
建造师(道具){
超级(道具);
this.state={copied:false};
this.componentDidMount=this.componentDidMount.bind(this);
}
componentDidMount(){
var clipboard=新剪贴板(“#复制按钮”);
剪贴板上的('success',函数(e){
this.setState({copied:true});
e、 选举();
});
剪贴板.on('error',函数(e){
document.getElementById(“title”).innerHTML='请手动复制';
});
}
手变(活动){
event.preventDefault();
event.target.select();
}
render(){
const EventURL=GenerateEventUrl(this.props.EventName,this.props.EventTimeUTC);
返回(
{this.state.copied?“copied!”:“做得很好。”}
现在,只需共享下面的链接。
它将显示{''} {' '} 在当地时间的任何人访问它

复制 ); }
}
您需要绑定将组件引用到函数的
。改变

function (e) {
    this.setState({copied: true});
    e.clearSelection();
}

或者使用ES6 arrow函数,它会自动绑定

e => {
    this.setState({copied: true});
    e.clearSelection();
}
令人惊叹的!非常感谢。
e => {
    this.setState({copied: true});
    e.clearSelection();
}