gpt4 book ai didi

javascript - Redux saga 永远不会运行

转载 作者:行者123 更新时间:2023-12-02 20:52:50 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中配置 redux-saga v1.1.3。

我使用自述文件 here我的配置如下。

在我的项目中:

npm i redux-saga@1.1.3 --save

我创建了rootSaga.js如下:

import { all } from 'redux-saga/effects'

export default (customSagas) => function* rootSaga() {
yield all([
...customSagas,
])
}

I turned the rootSaga.js into a function, because the rootSaga is within the core of my application and I needed to be able to add more customSaga to my application.

这是我更新的 configureStore.js:

import { routerMiddleware } from 'connected-react-router';
import {
createStore,
applyMiddleware,
compose,
} from 'redux';
import { persistReducer, persistStore } from 'redux-persist';
+ import createSagaMiddleware from 'redux-saga';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import { augmentStore } from '@nfen/redux-reducer-injector';

import storage from './storage';
import combineAppReducers from '../reducers';
+ import rootSagas from '../sagas';

function setupCreateReducer(customReducers = {}, reducerConfig) {
return function createReducer() {
return combineAppReducers({
...customReducers,
}, reducerConfig);
};
}

export default function configureStore(customReducers, customSagas, initialState = {}, persistConfig = {}, reducerConfig = {}) {
const runtimePersistConfig = {
key: 'root',
stateReconciler: autoMergeLevel2,
storage,
...persistConfig,
whitelist: [
'preferences',
...(persistConfig.whitelist || []),
],
};

let composeEnhancers = compose;
const reduxSagaMonitorOptions = {};
/* istanbul ignore next */
if (__DEV__ && typeof window === 'object') { // eslint-disable-line no-undef
/* eslint-disable no-underscore-dangle */
if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
}
/* eslint-enable */
}

+ const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
- const middlewares = combineAppMiddlewares([], reducerConfig.history);
+ const middlewares = combineAppMiddlewares([sagaMiddleware], reducerConfig.history);


- const middlewares = [routerMiddleware(reducerConfig.history)];
+ const middlewares = [sagaMiddleware, routerMiddleware(reducerConfig.history)];

const enhancers = [applyMiddleware(...middlewares)];

const createReducer = setupCreateReducer(customReducers, reducerConfig);
const persistedReducer = persistReducer(runtimePersistConfig, createReducer());
const store = createStore(
persistedReducer,
initialState,
composeEnhancers(...enhancers),
);
// Extensions
+ store.runSaga = sagaMiddleware.run;
+ store.runSaga(rootSagas(customSagas));

augmentStore(createReducer, store);
const persistor = persistStore(store);
return { store, persistor };
}

In there, I use redux and redux-persist, I also configure redux development tools. and have some core features such as preferences being preconfigured and persistent

这是我的customSagas.js:

import sessionsSaga from './sessionsSaga';

export default [
sessionsSaga,
];

这是我的sessionSaga.js:

import { takeLatest } from 'redux-saga/effects';

export function* logout() {
console.log('hello world');
}

export default function* logoutSaga() {
yield takeLatest('LOGOUT' , logout);
}

这就是我在 JSX 中调用传奇的方式:


function App() {
return (
<Button
push={() => dispatch({ type: 'LOGOUT' })}
>
Logout Saga
</Button>
);
}

我可以看到在我的 redux 开发工具中调度了 LOGOUT 类型,但我看不到 hello world。

我哪里失败了?

最佳答案

当你在 rootSagayield all([...])时,你必须调用sessionSaga( )。在您的代码中,您只是列出了生成器函数。请参阅root saga docs

我想是这样的:

import { all, call } from 'redux-saga/effects'

export default (customSagas) => function* rootSaga() {
yield all(customSagas.map(saga => call(saga)));
}

可以工作。

关于javascript - Redux saga 永远不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61561990/

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