Mil*_*des 2 redux-saga redux-toolkit
我一直在尝试将 redux sagas 和 redux 工具包引入我的项目。我目前遇到的问题是观察者传奇没有捕捉到takeEvery效果中的调度动作并运行处理程序。我看不出代码有什么问题。谁能帮忙!!!
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'
import createSagaMiddleware from 'redux-saga'
import logger from 'redux-logger';
import createReducer from './rootReducer';
import sagas from './rootSaga';
const configureAdminStore = (initialState = {}) => {
const sagaMiddleware = createSagaMiddleware();
// sagaMiddleware: Makes redux-sagas work
const middlewares = [sagaMiddleware, logger];
const store = configureStore({
reducer: createReducer(),
middleware: [...getDefaultMiddleware({thunk: false}), ...middlewares],
preloadedState: initialState,
devTools: process.env.NODE_ENV !== 'production',
});
sagaMiddleware.run(sagas);
return store;
}
export default configureAdminStore;Run Code Online (Sandbox Code Playgroud)
import {put, take, takeEvery, call} from 'redux-saga/effects'
import {getAll} from './environmentSlice'
import {confApi} from '../../service/conf-api'
import { getData } from '../../lib/conf-api-response';
function* getAllEnvironments() {
const response = yield call(confApi.admin.getEnvironments());
const {environments} = yield call(getData(response));
yield put(getAll(environments));
}
// eslint-disable-next-line import/prefer-default-export
export function* watchGetAllEnvironments() {
yield takeEvery(getAll().type, getAllEnvironments);
}Run Code Online (Sandbox Code Playgroud)
import { createSlice } from '@reduxjs/toolkit'
const environmentSlice = createSlice({
name: 'environments',
initialState: [],
reducers: {
getAll: (state, action) => {
state = action.payload
},
},
})
export const {getAll} = environmentSlice.actions
export const { getAllSuccess } = environmentSlice.actions;
export default environmentSlice.reducer
export const environmentSelector = (state) => state.environmentsRun Code Online (Sandbox Code Playgroud)
import {all} from 'redux-saga/effects'
import {watchGetAllEnvironments} from './environments/environmentSaga'
export default function* rootSaga() {
yield all([
watchGetAllEnvironments(),
])
}Run Code Online (Sandbox Code Playgroud)
如果您对创建可以解析/拒绝异步 thunk 操作的saga感兴趣,请查看saga-toolkit包 - 我创建并使用。
slice.js
import { createSlice } from '@reduxjs/toolkit'
import { createSagaAction } from 'saga-toolkit'
const name = 'example'
const initialState = {
result: null,
loading: false,
error: null,
}
export const fetchThings = createSagaAction(`${name}/fetchThings`)
export const doSomeMoreAsyncStuff = createSagaAction(`${name}/doSomeMoreAsyncStuff`)
const slice = createSlice({
name,
initialState,
extraReducers: {
[fetchThings.pending]: () => ({
loading: true,
}),
[fetchThings.fulfilled]: ({ payload }) => ({
result: payload,
loading: false,
}),
[fetchThings.rejected]: ({ error }) => ({
error,
loading: false,
}),
},
})
export default slice.reducer
Run Code Online (Sandbox Code Playgroud)
sagas.js
import { call } from 'redux-saga/effects'
import { takeLatestAsync, takeEveryAsync, putAsync } from 'saga-toolkit'
import API from 'hyper-super-api'
import * as actions from './slice'
function* fetchThings() {
const result = yield call(() => API.get('/things'))
const anotherResult = yield putAsync(actions.doSomeMoreAsyncStuff()) // waits for doSomeMoreAsyncStuff to finish !
return result
}
function* doSomeMoreAsyncStuff() {
...
return 'a value for another result'
}
export default [
takeLatestAsync(actions.fetchThings.pending, fetchThings), // takeLatestAsync: behaves a bit like debounce
takeEveryAsync(actions.doSomeMoreAsyncStuff.pending, doSomeMoreAsyncStuff), // each action will start a new saga thread
]
Run Code Online (Sandbox Code Playgroud)
看起来您只花了getAll().type两次 - 一次 inwatchGetAllEnvironments和一次 in getAllEnvironments。
这意味着watchGetAllEnvironments将执行getAllEnvironments,但会立即暂停并等待另一个getAll调度另一个操作,这可能永远不会发生。
所以你可能想先删除take它getAllEnvironments。
另外,你可以take(getAll),没有必要take(getAll().type)。
| 归档时间: |
|
| 查看次数: |
11177 次 |
| 最近记录: |