danielr18 / connected-next-router

A Redux binding for Next.js Router compatible with Next.js.
MIT License
109 stars 30 forks source link

Redux Toolkit #83

Open iot-cpars opened 2 years ago

iot-cpars commented 2 years ago

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..

Nigel-Skeels commented 2 years ago

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

Thank you

nimbit-software commented 2 years ago

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

halfmatthalfcat commented 1 year ago
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);
  },
});