gpt4 book ai didi

redux - Redux 工具包中的多个切片中的操作

转载 作者:行者123 更新时间:2023-12-03 14:40:49 29 4
gpt4 key购买 nike

Redux 工具包文档提到在多个 reducer 中使用操作(或者更确切地说是操作类型)

First, Redux action types are not meant to be exclusive to a single slice. Conceptually, each slice reducer "owns" its own piece of the Redux state, but it should be able to listen to any action type and update its state appropriately. For example, many different slices might want to respond to a "user logged out" action by clearing data or resetting back to initial state values. Keep that in mind as you design your state shape and create your slices.



但是,“记住这一点”,鉴于工具包将切片名称放在每个 Action 类型的开头,实现这一点的最佳方法是什么?并且您从该切片中导出一个函数并调用该单个函数来调度操作?我错过了什么?这是否必须以某种不使用 createSlice 的方式完成? ?

最佳答案

看起来这就是 extraReducers是为了:

One of the key concepts of Redux is that each slice reducer "owns" its slice of state, and that many slice reducers can independently respond to the same action type. extraReducers allows createSlice to respond to other action types besides the types it has generated.



Action 调度器应该知道 Action 属于哪个reducer,这有点奇怪。我不确定拥有 reducers 的动机和 extraReducers,但你可以使用 extraReducers允许多个切片响应相同的操作。

关于redux - Redux 工具包中的多个切片中的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61263663/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com