目前我正在这样做,但这不是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) 我有一个自定义模态组件。当它打开时,背景中没有任何滚动。
我在下面尝试了这段代码:
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)