在运行Saga之前,你必须使用redux工具applyMiddleware将Saga中间件挂载到Store上。

问题描述 投票:0回答:1

谁能帮忙解决这个问题。Error: Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware

挂载了sagaMiddleware,但仍然在页面上出现错误 添加了redux-saga到Store,并使用saga Middleware Store Js。

import { createStore, applyMiddleware, compose } from "redux";
import { persistStore } from "redux-persist";
import createSagaMiddleware from 'redux-saga'
import {fetchCollectionsStart} from './shop/shop.sagas'
import logger from "redux-logger";
import rootReducer from "./root-reducer";

const sagaMiddleware = createSagaMiddleware();

const middlewares = [sagaMiddleware];

if (process.env.NODE_ENV === "development") {
  middlewares.push(logger);
}
const composeEnhancers =
  typeof window === "object" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
        // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
      })
    : compose;

const enhancer = composeEnhancers(
  applyMiddleware(...middlewares,sagaMiddleware)
  // other store enhancers if any
);

sagaMiddleware.run(fetchCollectionsStart);

export const store = createStore(rootReducer, enhancer);
export const persistor = persistStore(store);

创建这个saga是为了在组件挂载Sagas.js后获取集合。

import {takeEvery} from 'redux-saga/effects';
import ShopActionTypes from './shop.types';

export function* fetchCollectionsAsync(){
    yield console.log('Good');
}
export function* fetchCollectionsStart(){
    yield takeEvery(ShopActionTypes.FETCH_COLLECTIONS_START,
        fetchCollectionsAsync );
}
reactjs redux redux-saga redux-store
1个回答
1
投票

确保你运行 sagaMiddleware.run(fetchCollectionsStart); 之后 createStore

const { useState, useEffect } = React;
const { bindActionCreators, combineReducers, createStore, applyMiddleware, compose } = Redux;
const { connect, Provider } = ReactRedux;
const createSagaMiddleware = ReduxSaga.default;
const { takeEvery } = ReduxSagaEffects;
const sagaMiddleware = createSagaMiddleware();
const reducer = (state = {}, action) => {
  return state;
}

const enhancer = compose(
  applyMiddleware(sagaMiddleware)
);

const reducers = combineReducers({
  reducer
})

const store = createStore(
  reducers,
  enhancer
);

function* fetchCollectionsAsync(){
    yield console.log('Good');
}

function* fetchCollectionsStart(){
    yield takeEvery('FETCH_COLLECTIONS_START',
        fetchCollectionsAsync );
}

sagaMiddleware.run(fetchCollectionsStart);

const action = () => ({
  type: 'FETCH_COLLECTIONS_START'
})

const mapStateToProps = state => ({
  
})

const mapDispatchToProps = ({
  action
})

const _App = ({action}) => {
  useEffect(() => {
    action();
  }, [])
  
  return <div>
  </div>
}

const App = connect(mapStateToProps, mapDispatchToProps)(_App)

ReactDOM.render(
    <Provider store={store}>
      <App />
    </Provider>,
    document.getElementById('root')
  );
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.10.1/polyfill.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/redux.js"></script>
<script src="https://unpkg.com/react-redux@latest/dist/react-redux.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux-saga/1.1.3/redux-saga.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux-saga/1.1.3/redux-saga-effects.umd.js"></script>
<div id="root"></div>
© www.soinside.com 2019 - 2024. All rights reserved.