DEV Community

Discussion on: Best Redux Architecture

Collapse
 
markerikson profile image
Mark Erikson

Hi, I'm a Redux maintainer. Unfortunately, I have concerns with several of the things you've shown here in this post - the code patterns shown are the opposite of how we recommend people use Redux today.

You should be using our official Redux Toolkit package and following our guidelines for using Redux with TypeScript correctly. That will eliminate all of the hand-written action types, action creators, and a lot of the other code you've shown, as well as simplifying the store setup process. You're also splitting the code across multiple files by type, and we recommend keeping logic in a single-file "slice" per feature.

For example, this is what that "app slice" would look like with RTK + TS:

import { createSlice, PayloadAction } from "@reduxjs/toolkit";

interface AppSliceState {
  isArchitecturePerfect: boolean;
}

const initialState : AppSliceState = {
  isArchitecturePerfect: false
}

const appSlice = createSlice({
  name: "app",
  initialState,
  reducers: {
    appImproved(state, action: PayloadAction<boolean>) {
      state.isArchitecturePerfect = action.payload;
    }
  }
})

export const { appImproved } = appSlice.actions;

export default appSlice.reducer;
Enter fullscreen mode Exit fullscreen mode

Notice how few TS typedefs are needed. We just need one for the initial state, and one for the payload of this action, and that's it. Everything else is inferred.

Note that we also recommend using pre-typed React-Redux hooks as well.

Please switch to using the patterns we show in the docs - it'll make everything much simpler!

Collapse
 
pas8 profile image
Anatolii

Thanks for your feedback, yeah, for sure to simplify code we should use redux-toolkit, but my idea was to show how we can manually set up with typescript our redux structure, just as an example how to have custom control at redux, probably or most likely its over-coding, I guess that for complex logic it has the right to exist due to a lot of error handlers, I am so sorry if somebody was misled due to title of the story. So should I delete the story or change totally sm?