[英]Conditionally applying inline style in react
我在组件上做一些画布菜单以做出反应,现在它有条件地工作了。 所以我有一个状态:
constructor(props) {
super(props);
this.state = {isToggleOn: true};
this.toggleMenu = this.toggleMenu.bind(this);
}
componentDidMount() {
this.setState({isToggleOn: false});
}
toggleMenu() {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
当我的isToggleOn
状态为true且为false时,我想使我的css overflow:hidden
我想从body删除overflow:hidden
。 如何实现?
您可以在其中添加一个componentDidUpdate
生命周期挂钩,以检查isToggleOn
的状态是否已更改,并更新主体overflow
样式。
componentDidUpdate(prevProps, prevState) {
if (this.state.isToggleOn !== prevState.isToggleOn) {
document.body.style.overflow = this.state.isToggleOn ? 'hidden' : 'visible';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.