React 点击按钮显示div与隐藏div
.m-test{width: 100px;height: 100px;background-color: red;}
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {
isToggleOn: true,
dispaly: 'block'
};
// 这个绑定是必要的,使`this`在回调中起作用
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn,
display: prevState.isToggleOn ? 'none': 'block'
}));
}
render() {
return (
{this.state.isToggleOn ? 'ON' : 'OFF'}
);
}
}
ReactDOM.render(
,
document.getElementById('root')
);