gpt4 book ai didi

redux-toolkit 在来自另一个 thunk reducer 的同一切片中使用 actionCreater

转载 作者:行者123 更新时间:2023-12-04 10:51:50 26 4
gpt4 key购买 nike

我有一个使用 createSlice 生成的 redux-thunk reducer 来自名为 getOne 的 redux-toolkit .
getOne从 API 获取并调度加载状态的操作,( startedLoadingfinishedLoadingerrorLoading )。

我还想调用在同一切片中创建的另一个 actionCreater,名为 insert与结果数据。或直接从 getOne 更新状态 reducer 。

import { createSlice } from "@reduxjs/toolkit"
import { startedLoading, finishedLoading, errorLoading } from '../slices/loadingSlice'
const apiEndpoint = "/api/v1"
const fetchOptions = { headers: { "Content-Type": "application/json" } }

const createModelSlice = function (modelName) {
return createSlice({
name: modelName,
initialState: {byId: {}},
reducers: {
insert: (state, action) => {
// ...
},
getOne: (state, action) => async (dispatch) => {
// ...
try {
const response = await fetch(/* */)

// How would I update the store here?

// 1. reference the insert actionCreater somehow.
dispatch(insert({id: id, data: response))

// 2. construct the action manually
dispatch({action: `${modelName}/insert`, payload: {id: id, data: response))

// 3. Mutate the state here and rely immer. (I'm not sure exactly how that works)
state[modelName].byId[id] = response

dispatch(finishedLoading({ key: /* */ }))
} catch (error) {
dispatch(errorLoading({ key: /* */ }))
}
},
// ...
}
})
}

最佳答案

我错过了有关切片无法使用 thunk 的部分文档。无论哪种方式,它都不起作用,因为 thunk Action 不会映射到 reducer ,而是将其他 Action 分派(dispatch)到多个其他 reducer / Action 。

创建切片后,我向切片操作添加了 thunk 操作。这样我可以引用其他 Action


import { createSlice } from "@reduxjs/toolkit"
const slice = createSlice({
name: name,
initialState: { byId: {} },
reducers: { /* */ }
}
slice.actions.myThunkAction = payload => async (dispatch, state) => {
// ...
slice.actions.nonThunkAction({ id: id, data: data})
slice.actions.anotherNonThunkAction({ index payload.index, data: data.map( /* */ )})
}

import { createSlice } from "@reduxjs/toolkit"
import { startedLoading, finishedLoading, errorLoading } from '../slices/loadingSlice'
import encodeURLParams from '../tools/encodeURLParams'

const apiEndpoint = "/api/v1"
const fetchOptions = { headers: { "Content-Type": "application/json" } }

const createModelSlice = function (modelName) {
const slice = createSlice({
name: modelName,
initialState: { byId: {} },
reducers: {
insert: (state, action) => {
// ...
},
bulkInsert: (state, action) => {
// ...
},
}
})
slice.actions.loadMany = payload => async (dispatch, state) => {
dispatch(startedLoading({ key: /* */ }))
try {
const response = await fetch(/* */)
dispatch(slice.actions.insert(response))
dispatch(finishedLoading({ key: /* */ }))
} catch (error) {
dispatch(errorLoading({ key: /* */ }))
}
}
slice.actions.loadOne = payload => async (dispatch, state) => {
dispatch(startedLoading({ key: /* */ }))
try {
const response = await fetch(/* */)
dispatch(slice.actions.bulkInsert(response))
dispatch(finishedLoading({ key: /* */ }))
} catch (error) {
dispatch(errorLoading({ key: /* */ }))
}
}
return slice
}

export default createModelSlice

关于redux-toolkit 在来自另一个 thunk reducer 的同一切片中使用 actionCreater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59448133/

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