繁体   English   中英

有条件地在反应中应用内联样式

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM