connected-next-router icon indicating copy to clipboard operation
connected-next-router copied to clipboard

Redux Toolkit

Open iot-cpars opened this issue 3 years ago • 3 comments

Hi there, this is less of an issue and more of a request.

Could you maybe make an example of how to use this with redux.toolkit and slices?

I tryed getting it working but am rather new to redux.

Thanks a lot..

iot-cpars avatar Feb 21 '22 13:02 iot-cpars

I would be interested to see an example of this too.

Thank you

Nigel-Skeels avatar Jun 23 '22 10:06 Nigel-Skeels

What i ended up doing is wraping my getServerSideProps with a method that writes my access token into the Redux store and then i can read it out of the store in the prepareHeaders function. That way i can automate the token handling. But maybe there is a better cookie based way

nimbit-software avatar Jun 23 '22 15:06 nimbit-software

import { createSlice } from '@reduxjs/toolkit';
import { HYDRATE } from 'next-redux-wrapper';
import { initialRouterState, routerReducer } from 'connected-next-router';

export const routerSlice = createSlice({
  name: 'router',
  initialState: initialRouterState(),
  reducers: {},
  extraReducers: (builder) => {
    builder
      // for next-redux-wrapper hydration
      .addCase(HYDRATE, (state, { payload }) => ({
        ...state,
        ...payload.router,
      }))
      .addDefaultCase(routerReducer);
  },
});

halfmatthalfcat avatar Feb 22 '23 18:02 halfmatthalfcat