redux toolkit在另一个thunk reducer的同一片中使用actionCreater

redux toolkit在另一个thunk reducer的同一片中使用actionCreater,redux,redux-thunk,redux-toolkit,Redux,Redux Thunk,Redux Toolkit,我有一个redux thunk reducer,它是用redux工具包中的createSlice生成的,名为getOne getOne从API获取并分派加载状态的操作(startedLoading,finishedLoading,errorLoading) 我还想调用另一个actionCreater,该actionCreater使用生成的数据在同一个片段中创建,名为insert。或者直接从getOnereducer更新状态 从“@reduxjs/toolkit”导入{createSlice} 从

我有一个redux thunk reducer,它是用redux工具包中的
createSlice
生成的,名为
getOne

getOne
从API获取并分派加载状态的操作(
startedLoading
finishedLoading
errorLoading

我还想调用另一个actionCreater,该actionCreater使用生成的数据在同一个片段中创建,名为
insert
。或者直接从
getOne
reducer更新状态

从“@reduxjs/toolkit”导入{createSlice}
从“../slices/loadingSlice”导入{startedLoading,finishedLoading,errorLoading}
const apidendpoint=“/api/v1”
const fetchOptions={headers:{“Content Type”:“application/json”}
const createModelSlice=函数(modelName){
返回createSlice({
名称:modelName,
初始状态:{byId:{}},
减速器:{
插入:(状态、动作)=>{
// ...
},
getOne:(状态、操作)=>async(调度)=>{
// ...
试一试{
常量响应=等待获取(/**/)
//我如何更新这里的商店?
//1.以某种方式引用insert actionCreater。
分派(插入({id:id,数据:响应))
//2.手动构造操作
分派({action:`${modelName}/insert`,负载:{id:id,数据:响应))
//3.改变这里的状态,依靠伊默尔(我不确定这到底是怎么回事)
状态[modelName].byId[id]=响应
分派(已完成加载({键:/**/}))
}捕获(错误){
分派(错误加载({key://**}))
}
},
// ...
}
})
}

我错过了文档中关于切片无法使用thunk的部分。无论哪种方式,它都不起作用,因为thunk操作不会映射到一个减速器,而是将其他操作分派到多个其他减速器/操作

创建切片后,我将thunk动作添加到切片动作中。这样我可以引用其他动作


从“@reduxjs/toolkit”导入{createSlice}
const slice=createSlice({
姓名:姓名,,
初始状态:{byId:{}},
还原符:{/**/}
}
slice.actions.myThunkAction=payload=>async(调度,状态)=>{
// ...
slice.actions.nonThunkAction({id:id,data:data})
slice.actions.anotherNonThunkAction({index payload.index,data:data.map(/**/)})
}
从“@reduxjs/toolkit”导入{createSlice}
从“../slices/loadingSlice”导入{startedLoading,finishedLoading,errorLoading}
从“../tools/encodeURLParams”导入encodeURLParams
const apidendpoint=“/api/v1”
const fetchOptions={headers:{“Content Type”:“application/json”}
const createModelSlice=函数(modelName){
const slice=createSlice({
名称:modelName,
初始状态:{byId:{}},
减速器:{
插入:(状态、动作)=>{
// ...
},
bulkInsert:(状态、操作)=>{
// ...
},
}
})
slice.actions.loadMany=有效负载=>async(调度,状态)=>{
分派(已启动加载({key://**}))
试一试{
常量响应=等待获取(/**/)
分派(切片.操作.插入(响应))
分派(已完成加载({键:/**/}))
}捕获(错误){
分派(错误加载({key://**}))
}
}
slice.actions.loadOne=有效负载=>async(调度,状态)=>{
分派(已启动加载({key://**}))
试一试{
常量响应=等待获取(/**/)
调度(切片、动作、批量插入(响应))
分派(已完成加载({键:/**/}))
}捕获(错误){
分派(错误加载({key://**}))
}
}
返回片
}
导出默认createModelSlice