相关疑难解决方法(0)

何时,何地以及如何在使用React.js时将类添加到document.body

目前我正在这样做,但这不是react.js的方式,对吧?render()是正确的地方吗?有什么选择?

  var App = React.createClass({
    render: function() {
      if (this.state.touchMode === 2) {
          $('body').addClass('touchMode');
      }

      return (<div> etc /div>)
    }
  )}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

12
推荐指数
2
解决办法
9930
查看次数

反应:模式打开时防止滚动

我有一个自定义模态组件。当它打开时,背景中没有任何滚动。

我在下面尝试了这段代码:

componentDidMount() {
    document.body.style.overflow = 'hidden';
}

componentWillUnmount() {
    document.body.style.overflow = 'unset';
}
Run Code Online (Sandbox Code Playgroud)

乍看起来似乎可行,但是当我使用模式组件时,在另一页中,即使关闭了模式也没有滚动。

有更好的解决方案吗?

我的模态组件:

export class Modal extends React.Component {

constructor(props) {
    super(props);
}

componentDidMount() {
    document.body.style.overflow = 'hidden';
}

componentWillUnmount() {
    document.body.style.overflow = 'unset';
}

render() {
    return (
        <React.Fragment>
            {this.props.showAt ?
                this.props.show ?
                    <div style={style} className={`${this.props.sectionName} ${modalTypeStyle ? modalTypeStyle : styles.modalWhite} ${modalTypeSize ? modalTypeSize : styles.modalSmall} ${!this.props.showAt ? styles.modalWhiteFixed : ""}`}>
                        {this.props.arrowShape ? <div className={arrowTypeStyle ? arrowTypeStyle : styles.triangleToprightWhite} /> : null}
                        {this.props.children}
                    </div>
                    : …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

8
推荐指数
7
解决办法
2851
查看次数

标签 统计

javascript ×2

reactjs ×2