简体   繁体   English

如何在没有 typescript 的情况下将下一个 redux 包装器与 redux 工具包一起使用?

[英]How to use next redux wrapper with redux toolkit without typescript?

I have a redux store created using javascript, and I am unable to use next redux wrapper with it.我有一个使用 javascript 创建的 redux 商店,我无法使用下一个 redux 包装器。

Here is my slice:这是我的切片:

import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { HYDRATE } from "next-redux-wrapper";

export const getCurrentUser = createAsyncThunk(
  "counter/getCurrentUser",
  async () => {
    const res = await axios.get("http://localhost:8000/get-current-user", {
      withCredentials: true,
    });
    return res.data;
  }
);

const userSlice = createSlice({
  name: "user",
  initialState: {
    username: null,
    email: null,
    firstName: null,
    lastName: null,
    gender: null,
    dob: null,
  },
  reducers: {
    changeUser: (state, action) => {
      return action.payload;
    },
    clearUser: () => {
      return {
        username: null,
        email: null,
        firstName: null,
        lastName: null,
        gender: null,
        dob: null,
      };
    },
  },
  // extraReducers(builder) {
  //   builder.addCase(getCurrentUser.fulfilled, (state, action) => {
  //     return action.payload;
  //   });
  // },
  extraReducers: {
    HYDRATE: (state, action) => {
      console.log("HYDRATE", state, action.payload);
      return {
        ...state,
        ...action.payload.subject,
      };
    },
  },
});

export const { changeUser, clearUser } = userSlice.actions;

export default userSlice.reducer;

Here is my store code:这是我的商店代码:

// TODO add HYDRATE wrapper nextjs
import { configureStore } from "@reduxjs/toolkit";
import userReducer from "./userSlice";
import emailReducer from "./emailSlice";
import { createWrapper } from "next-redux-wrapper";

const makeStore = () =>
  configureStore({
    reducer: {
      user: userReducer,
      email: emailReducer,
    },
    devTools: true,
  });

export const wrapper = createWrapper(makeStore);

Here is my _app.js component:这是我的_app.js组件:

function MyApp({ Component, ...rest }) {
  const router = useRouter();
  const { store, props } = wrapper.useWrappedStore(rest);
  const { pageProps } = props;

  return (
    <>
      <Provider store={store}>
        <Header />
        <AnimatePresence initial={false} mode="wait">
          <App>
            <Component {...pageProps} key={router.pathname} />
          </App>
        </AnimatePresence>
      </Provider>
    </>
  );
}

const App = ({ children }) => {
  const dispatch = useDispatch();

  useEffect(() => {
    console.log("Calling dispatch in backend");
    dispatch(getCurrentUser());
  });
  return <>{children}</>;
};

export default wrapper.withRedux(MyApp);

This code does not work, it does not call the HYDRATE in the extra reducers.此代码不起作用,它不会在额外的减速器中调用HYDRATE Also I want to add builder cases to the extra reducers function, how can I do it while using HYDRATE?我还想将生成器案例添加到额外的减速器 function,在使用 HYDRATE 时我该怎么做? What am I doing wrong here?我在这里做错了什么?

You can just do你可以做

   extraReducers(builder) {
     builder.addCase(HYDRATE, (state, action) => {
       // your logic here
     });
   },

I am not sure what your question has to do with TypeScript, as you mention TS in the title.正如您在标题中提到的 TS,我不确定您的问题与 TypeScript 有什么关系。 Maybe there is another part to your question that is not clear to me - in that case, please specify.也许您的问题的另一部分我不清楚 - 在这种情况下,请说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确使用带有“Next.js”、“Redux-ToolKit”和 Typescript 的“next-redux-wrapper”? - How to use "next-redux-wrapper" with "Next.js", "Redux-ToolKit" and Typescript properly? 如何使用 typescript、next-redux-wrapper、getServerSideProps? - how to use typescript, next-redux-wrapper, getServerSideProps? 如何在 SPFX (typescript) 项目中使用 redux 工具包? - How to use redux toolkit in SPFX (typescript) project? 仅执行“静态生成”时,是否真的需要在“Next.js + Redux Toolkit”应用程序中使用“next-redux-wrapper”? - Is it really required to use 'next-redux-wrapper' in a 'Next.js + Redux Toolkit' application when doing only 'static generation'? 如何在 Next.js SSG 或 SSR 中使用 Redux 工具包调度? - How to use Redux toolkit dispatch in Next.js SSG or SSR? Typescript 和 redux 工具包 - Typescript and redux toolkit 如何在 redux 工具包(带打字稿)中设置 preloadedState? - How to set the preloadedState in redux toolkit (with typescript)? 如何修复 redux-toolkit 中的 typescript 错误? - How to fix typescript error in redux-toolkit? 如何将 Redux-Thunk 与 Redux Toolkit 的 createSlice 一起使用? - How to use Redux-Thunk with Redux Toolkit's createSlice? 如何在 Redux 工具包中将 Redux Promise 中间件与切片一起使用? - How to use Redux Promise Middleware with slices in Redux Toolkit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM