【发布时间】:2019-03-14 03:20:52
【问题描述】:
我正在基于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。
【问题讨论】: