IronTony/react-native-redux-toolkit-starter-app

Switch Redux setup to use Redux Toolkit

markerikson opened this issue · 3 comments

Hi, I'm a Redux maintainer. I'd strongly recommend switching the Redux setup and logic in this project to use our new official Redux Toolkit package. It includes utilities to simplify several common Redux use cases, including store setup, defining reducers, immutable update logic, and even creating entire "slices" of state at once:

https://redux-toolkit.js.org

edit

Actually, now that I look further, it seems that the utils/redux.ts file has copy-pasted some of the types and functions from Redux Toolkit. Why not just use Redux Toolkit directly? Also, why not use the createSlice() function from RTK instead of createReducer + createAction ?

Hi @markerikson ,

Thanks for your feedback, we really appreciate it and didn't expected it 😊 .

When we integrated redux into this starter app we considered using RTK but after some discussion we found that it wouldn't fit correctly into our basic idea.

The main reasons that brought us to this decision are the following:

  • we decided to avoid the ducks folder structure for this project and to stick to a more "separated" way of handling redux modules by keeping leaner and cleaner files divided by actions, reducers, sagas and selectors.
  • we decided to use redux-saga instead of redux-thunk and that would have required additional setup steps to be used together with RTK.

So we started out that way and later found out how to integrate some part of RTK, we love the idea and cleanness of using createAction and createReducer (and also implemented something similar to handle redux-saga related methods), but still found unnecessary to add a whole dependency to just use half of it.

FWIW, we do specifically recommend "feature folders" or "ducks", and createSlice makes it really easy to write ducks.

Also, adding sagas to an RTK setup can be done with:

configureStore({
    reducer,
    middleware: [...getDefaultMiddleware(), sagaMiddleware]
})

Not a lot of additional setup work there.

Hi @markerikson ,

thank you for the opened issue, and also for the hints. Very appreciated 😍
Based on your hint, we will import Redux-toolkit and we will modify our store.ts file and the related others.

About the folders and files structure inside the redux folder, we prefer to have all files (actions/reducers/sagas/selectors) splitted to avoid long files with all code from different types in a single file just for a matter of readability.