It's even messier when you have to manage request lifecycles, side effects, shared global state, and 3 to 4 boilerplate files in order to manage 1 data resource. This guide aims to provide a consistent pattern intended to increase productivity and reduce entropy long term.
Typically, there are 2 common patterns implemented in redux-world that you will find in the wild:
Pattern A: Conventions suggested by the official redux guides
Pattern B: Place all redux-related files inside of the corresponding component folder
Pattern A: Official redux convention
โโโ actions
โ โโโ cats.js
โ โโโ todos.js
โ โโโ users.js
โโโ components
โ โโโ cats
โ โ โโโ Form.jsx
โ โ โโโ ViewAll.jsx
โ โโโ todos
โ โ โโโ Form.jsx
โ โ โโโ ViewAll.jsx
โ โโโ users
โ โโโ Form.jsx
โ โโโ ViewAll.jsx
โโโ constants
โ โโโ cats.js
โ โโโ todos.js
โ โโโ users.js
โโโ reducers
โ โโโ cats.js
โ โโโ index.js
โ โโโ todos.js
โ โโโ users.js
โโโ sagas
โ โโโ cats.js
โ โโโ todos.js
โ โโโ users.js
โโโ index.js
Pattern B: Component scoped convention
โโโ components
โ โโโ cats
โ โ โโโ Form.jsx
โ โ โโโ ViewAll.jsx
โ โ โโโ actions.js
โ โ โโโ constants.js
โ โ โโโ reducer.js
โ โ โโโ sagas.js
โ โโโ todos
โ โ โโโ Form.jsx
โ โ โโโ ViewAll.jsx
โ โ โโโ actions.js
โ โ โโโ constants.js
โ โ โโโ reducer.js
โ โ โโโ sagas.js
โ โโโ users
โ โโโ Form.jsx
โ โโโ ViewAll.jsx
โ โโโ actions.js
โ โโโ constants.js
โ โโโ reducer.js
โ โโโ sagas.js
โโโ index.js
Both ways work to organize the code and are fine. We can apply a similar structure to saga slices:
In either case, it is cleaner to separate redux store, react config, and module declarations into separate files. This allows us to leave each file to perform its' intended purpose. For example:
Both should only worry about importing the saga slice modules and not couple store instantiation logic inside of here. This allows for this file to be the source of truth for declaring saga slices. It can grow as much as it needs to without logic clutter.
Wiring up React, and wiring up redux can both become a stringy, tangled mess. This is why it is wise to separate the two.
File./store.js
Should only contain redux logic. It should not be coupled with react logic at all. The helps us grow the logic needed for redux in particular away from React.
This file should be focused on your root level application logic relating to react. This includes any routing you might want to implement, HOCs, and containers that need to wrap your application logic.