雷杜克斯.从容器组件描述表示组件的生命周期方法
Redux. Describe presentational component's lifecycle methods from a container component
目前我有两个组件(一个容器组件和一个演示组件)。我有一些逻辑应该在componentDidMount
和componentWillReceiveProps
生命周期方法中实现,特别是我必须在那里调度一些操作。
现在,我正在mapDispatchToLinkProps
函数中映射这些动作创建者,并使用connect
函数将它们传递给表示组件的props
,然后,我在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)
有没有办法从容器组件描述表示组件的生命周期方法,但保留表示组件的上下文?或者更好的是,在表示组件中没有任何调用的情况下重写生命周期方法?
抱歉,
我还不能添加评论,
为什么不在表示组件中进行必要的检查,然后使用正确的参数进行调用?
前任:
componentWillReceiveProps(nextProps) {
if (this.props.floor != nextProps.floor) {
this.props.loadRooms(nextProps.floor);
}
}
容器组件 :
const mapDispatchToLinkProps = (dispatch, ownProps) => {
return {
onMount: () => {
dispatch(loadRooms(ownProps.floor))
},
loadRooms: (floor) => {
dispatch(loadRooms(floor));
}
}
}
你所要求的是可以通过一些黑客来实现的,但它违背了反应和redux的哲学。
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- React组件等待所需道具进行渲染
- React中的数据集表示
- 如何创建带有插槽的vue js组件预加载程序
- KnockoutJS-组件-多个实例
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在react js中将值从一个组件发送到另一个组件
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- React组件-设置页面标题
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何在salesforce aura闪电组件中使用jQuery.val()
- v表示动态组件vuejs1.0
- 我可以将 AJAX 调用放在表示组件中,还是应该提取容器
- 雷杜克斯.从容器组件描述表示组件的生命周期方法
- Redux - 繁重的工作应该在哪里发生 - 化简器,动作,容器或表示组件
- 如何有效地将相同的“道具”传递给每个无状态表示组件
- 分离表示和逻辑组件反应/重复
- 如何更新表示相同数据的两个不同React组件实例