我的应用程序处于可能没有user
对象的状态。
我有两个容器和两个组件:
ParentContainer:
const mapStateToProps = (state) => ({
showUserDetails: Boolean(state.user),
})
export default connect(mapStateToProps)(ParentComponent)
ParentComponent:
const ParentComponent = ({ showUserDetails }) => (
<div>
{showUserDetails && <ChildContainer />}
</div>
)
子容器:
const mapStateToProps = (state) => ({
name: state.user.name,
})
export default connect(mapStateToProps)(ChildComponent)
ChildComponent:
const ChildComponent = ({ name }) => (
<h1>{name}></h1>
)
但是,我遇到的场景是,在设置了某个动作之后state.user = null
,ChildContainer
尝试在此之前渲染,ParentContainer
并由于无法访问而引发异常null.name
。
Redux的这种预期行为是子容器可以在父容器之前重新呈现吗?在经典的React流中,不会发生此错误。
我很欣赏这个人为的例子一个可能只需要调用<ChildComponent name={user.name} />
的ParentComponent
,而不是使用一个容器。但是在现实世界中,我具有嵌套的组件,这些组件访问许多状态的不同部分,因此不能轻易地传递道具。
您要分批处理Redux派遣吗?如果不仔细阅读v3.0.0发行说明,请执行以下操作:
https://github.com/reactjs/react-redux/releases/tag/v3.0.0
它建议使用redux-batched-updates
模块,但已过时或不推荐使用redux-batched-subscribe
像这样使用它:
import { unstable_batchedUpdates as batchedUpdates } from 'react-dom';
import { batchedSubscribe } from 'redux-batched-subscribe';
const store = createStore(reducer, intialState, batchedSubscribe(batchedUpdates));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句