我根据display
属性有条件地渲染模态组件。
我需要在组件show
/上实现切换正文滚动功能hide
。
见下面的实现,
演示组件
<button onClick={()=> this.setState({ show: true })}>Show modal</button>
<Modal show={show} containerStyle={containerStyle} position={position} handleClickOutside={()=> this.setState({ show: false })} >
<Content />
</Modal>
模态组件
componentDidMount() {
disableBodyScroll(this.state.defaultMargin);
}
componentWillUnmount() {
enableBodyScroll(this.state.defaultMargin);
}
render() {
const { show } = this.props;
const display = show ? 'block' : 'none';
return (
<div onClick={ handleClickOutside } className={ styles[ 'modal'] } style={ { display } }>
{children}
</div>
);
}
但问题是在显示模态之前调用了 componentDidMount 函数。我希望在显示模态后调用它
隐藏 Modal 时应调用 componentWillUnmount。