目前我有兩個組件(一個容器組件和一個演示文件)。我有一些邏輯應該在componentDidMount
和componentWillReceiveProps
生命週期方法中實施,特別是我必須在那裏發送一些操作。Redux。從容器組件描述表示組件的生命週期方法
現在,我映射那些行動的創作者在mapDispatchToLinkProps
功能,並傳遞給我的表象組件的props
用connect
功能,事後,我給他們打電話componentDidMount
和componentWillReceiveProps
方法。這裏是如何,我實現它:
容器組件:
const mapDispatchToLinkProps = (dispatch, ownProps) => {
return {
onMount:() => {
dispatch(loadRooms(ownProps.floor))
},
onPropsReception: (nextProps, currentProps) => {
if (nextProps.floor != currentProps.floor) {
dispatch(loadRooms(nextProps.floor))
}
}
}
}
表象成分:
export default connect(mapStateToProps, mapDispatchToLinkProps)(MapLayer):
componentDidMount() {
this.props.onMount()
}
componentWillReceiveProps(nextProps) {
this.props.onPropsReception(nextProps, this.props)
}
render() {
// ...
}
,我不喜歡這裏的事情,是我以這種方式丟失上下文,我必須通過this.props
作爲第二個參數onPropsReception
方法:
this.props.onPropsReception(nextProps, this.props)
有沒有一種方法可以從容器組件描述表示組件的生命週期方法,但是保持表示組件的上下文?或者甚至更好,覆蓋生命週期方法而無需在表示組件中調用?
你能解決這個問題嗎?我有類似的問題。我正在使用react路由器和redux。我正在捕捉url查詢參數(/ search?q ='foo'&page = 1)。當url參數更改時,將調用componentWillUpdate生命週期方法。我不知道使用connect方法傳遞生命週期組件的方式。或者,如果我如何將mapStateToProp傳遞給React.createClass組件。 –
對不起,沒有時間回到這裏,沒有解決問題。 –