目前我有两个组件(一个容器组件和一个演示文件)。我有一些逻辑应该在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组件。 –
对不起,没有时间回到这里,没有解决问题。 –