[英]How to do something when modal is shown in React.js?
我正在使用react.js
, redux
和redux-modal
。 我想在顯示模態時執行代碼。
當我將代碼放入componentWillMount
,它只是在第一次執行。
componentWillMount() {
// Just execute one time
// Do something
}
redux-modal
使用show
state變量顯示或隱藏modal,因此我使用以下代碼來處理show事件:
componentWillReceiveProps(nextProps) {
// When modal is shown
if (!this.props.show && nextProps.show) {
// Do something
}
}
除了第一次安裝模態外,它運行良好。
現在,我一起使用componentWillMount
和componentWillReceiveProps
來處理模式展示事件。
有沒有更好的解決方案來處理模態表演事件?
您可以簡單地將代碼放入渲染函數中,如下所示:
render() {
{(this.props.show) ? <MyModalComponent/> : null}
... // other components to render
}
如果您使用componentDidMount()
觸發API調用,那么使用上面的代碼,您的流程將是:
this.props.show == false
render()
,組件將呈現無模式 componentDidMount()
運行,從而觸發API調用 show
設置為true this.props.show == true
render()
,組件將被模態渲染 componentDidMount()
未運行,因為這是第二個渲染)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.