多次调用 Redux saga(2 或 3 次)Nextjs

Redux saga called multiple times (2 or 3 times) Nextjs

我将 Redux 和 Redux saga 与 Nextjs 一起使用,我将商店包装在 _app.js 文件中,当我用 post 调用 api 或获取请求时,Redux-Saga 正在获取至少调用两次,特别是针对 post 请求,例如,如果我想使用 api 注册用户,它会在数据库中注册用户两次 PS:我正在使用 rootSaga,我不会在那里调用 saga 两次 这是我的商店文件:

import { createStore, applyMiddleware, compose } from "redux";
import createSagaMiddleware from "redux-saga";
import reducers from "./reducers";

import sagas from "./sagas";

const sagaMiddleware = createSagaMiddleware();

const middlewares = [sagaMiddleware];
export function configureStore(initialState) {
  const store = createStore(
    reducers,
    initialState,
    compose(applyMiddleware(...middlewares))
  );

  sagaMiddleware.run(sagas);

  if (module.hot) {
    module.hot.accept("./reducers", () => {
      const nextRootReducer = require("./reducers");
      store.replaceReducer(nextRootReducer);
    });
  }

  return store;
}

export const wrapper = createWrapper(configureStore, { debug: true });

这是我的 _app.js 文件

import "../styles/styles.global.scss";
import "../styles/Home.module.scss";
import React from "react";

import App, { Container } from "next/app";
import { Provider, connect } from "react-redux";

import withRedux from "next-redux-wrapper";
import { configureStore, wrapper } from "../libs/store";
const context = React.createContext(undefined);


class MyApp extends App {
  static async getInitialProps({ Component, ctx }) {

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    return { ...pageProps };
  }

  render() {
    const { Component, pageProps, router } = this.props;
    return (
      <Provider store={configureStore()} context={context}>
        <Component {...pageProps} key={router.asPath} />
      </Provider>
    );
  }
}
export default wrapper.withRedux(MyApp);

谢谢。

我通过从 _app.js 中删除提供商并删除 _document.js 来修复它 PS:此解决方案适用于 Nextjs >= 10.0.0