从Redux存储更新组件状态

时间:2020-07-16 12:55:11

标签: reactjs redux

我从redux存储一些配置

dispatch(aOrder.getThrottlingConfig(storeId))

之后,我想将redux的值设置为组件存储

const throttlingConfig: IOrderConfig = useSelector<RootState, IOrderConfig>(({ root }) => root.order.throttling)
setCapacity(throttlingConfig.throttling.context.capacity)

但是当未定义组件安装限制时,请尝试使条件成立

if (throttlingConfig.throttling) {
     setCapacity(throttlingConfig.throttling.context.capacity)
    }

它永远不会执行,我在做什么错? 当他们需要的字段更新时,我只需要Redux存储中的setState

1 个答案:

答案 0 :(得分:1)

在此块中,您将root.order.throttling分配给throttlingConfig,然后在访问它时尝试访问throttlingConfig.throttling.context.capacity

const throttlingConfig: IOrderConfig = useSelector<RootState, IOrderConfig>(({ root }) => root.order.throttling) setCapacity(throttlingConfig.throttling.context.capacity)

实际上是root.order.throttling.throttling.context.capacity

因此,假设您的数据结构符合我的预期,则应该可以正常工作:

if (throttlingConfig) {
 setCapacity(throttlingConfig.context.capacity)
}