我已经有一个简单的切换组件反应,即时通讯相当不知道为什么它不会工作。另外,如果即时通讯使用箭头功能,我还需要绑定(这)?简单的开/关切换组件不工作
class MyComponent extends React.Component {
construtor(props){
super(props);
this.state = {visibility: false};
}
toggleVisibility =() => {
this.setState({
visibility: !this.state.visibility
});
}
render() {
if(this.state.visibility) {
return (
<div>
<button
onClick={this.toggleVisibility}>Click</button>
<h1>now you see me</h1>
</div>
);
} else {
return(
<div>
<button
onClick={this.toggleVisibility}>Click</button>
</div>
);
}
}
};
ReactDOM.render(<MyComponent />, document.getElementById("root"));
当您提出问题时,请提供更多关于您遇到的问题的详细信息,而不是“不起作用”。错误消息,控制台日志等。它让我们有更多的需要。 – jered
不要在'setState'中使用'this.state'。 https://facebook.github.io/react/docs/react-component.html#setstate – SLaks
我正在写codepen,它给了我“toggle unexpectedness =”在toggleVisibility方法下,我猜是使用箭头函数 – Sotero