|
| 1 | +# The Flux architectural pattern using `useReducer`, the `Context` API, and Sagas |
| 2 | + |
| 3 | +Just in case, here is a nice diagram about the generic Flux pattern in React. |
| 4 | + |
| 5 | +<img src="public/images/flux-pattern.png" alt="Flux architectural pattern"> |
| 6 | + |
| 7 | +## So, why does this repo even exist? |
| 8 | + |
| 9 | +It demonstrates the combination of : |
| 10 | + |
| 11 | +- `Context` API and `useContext` hook |
| 12 | +- `useReducer` hook |
| 13 | +- [`useReactSaga` custom hook](https://www.npmjs.com/package/use-react-saga) |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +[Demo Link](https://confident-turing-f05f43.netlify.app/) | [GitHub Repo](https://github.com/boostup/react-context-api-multiple-reducers-mulitple-sagas) |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## But why, you ask ? |
| 22 | + |
| 23 | +Because I can ;) |
| 24 | + |
| 25 | +No, but seriously, because I needed to make sense of Redux, of the Context API, and more generally, the Flux Pattern, and of async actions. |
| 26 | + |
| 27 | +Also, to me, it is important to understand as many React functions or third-party packages as I include in my apps, and `useContext` and `useReducer` being shipped defacto with React is a plus, versus having to include a whole bunch of packages that revolve around the Redux ecosystem. |
| 28 | + |
| 29 | +So this demo allows to demonstrate that the `redux` and `react-redux` packages are no longer required dependencies to implement the Flux Pattern in React, in a way that makes both sense and is scalable with combined reducers to handle endless future slices of state. |
| 30 | + |
| 31 | +## Word of caution |
| 32 | + |
| 33 | +What this demo does not demonstrate though, is if this combination mimicking redux will be as performant as redux. |
| 34 | +And this, I do not know myself, as this is very experimental. I wouldn't recommend using this in production ;). |
| 35 | + |
| 36 | +## Notes |
| 37 | + |
| 38 | +This demo depends on a tiny package called `use-react-saga`. |
| 39 | + |
| 40 | +All it is, is a custom hook for React which depends on the `redux-saga` package, which is something I wanted to experiment with as the next natural evolution step after `redux-thunk`, for async actions. |
| 41 | + |
| 42 | +## Addendum |
| 43 | + |
| 44 | +After the perusal of the installed packages in the `node_modules` directory, I have come to realize that there is a `redux` directory in there!! After perusing the `yarn.lock` file, `redux-saga` packages has it as a dependency. |
| 45 | + |
| 46 | +So this demo does NOT unfortunately demonstrate that adding Redux as a dependency can be avoided. Fortunately, it is only [2kB, including dependencies](https://www.npmjs.com/package/redux)! |
| 47 | + |
| 48 | +So this demo only goes to shows how **unavoidable** `redux` is when it comes to making stable apps ;) |
| 49 | + |
| 50 | +In the particular case of this demo, if I wish to remove it as a dependency, I must implement Sagas myself. Is it worth it ? I'll leave it up to you to decide. |
| 51 | + |
| 52 | +Finally, I should mention that I do not hold a grudge against Redux, I just wanted to evaluate the Context API as a potential replacement, since we see so many articles on this topic, but they all show very small unscalable demos or POCs which were never convincing to me. |
| 53 | + |
| 54 | +--- |
| 55 | + |
1 | 56 | # Getting Started with Create React App |
2 | 57 |
|
3 | 58 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
|
0 commit comments