【问题标题】:Redux saga called multiple times (2 or 3 times) NextjsRedux saga 调用多次(2 或 3 次)Nextjs
【发布时间】:2021-06-22 12:01:54
【问题描述】:

我将 Redux 和 Redux saga 与 Nextjs 一起使用,我将商店包装在 _app.js 文件中,当我使用 post 或 get 请求调用 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);

谢谢。

【问题讨论】:

    标签: reactjs redux next.js redux-saga


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 2019-08-02
      • 2018-09-06
      • 1970-01-01
      • 2019-12-01
      • 2016-10-12
      • 2019-12-03
      相关资源
      最近更新 更多