Redux’s ideology is a unidirectional data flow. This pattern reduces long term complexity, encourages re-usability, and generally discourages spaghetti code. 🍝
Video Series: https://egghead.io/courses/getting-started-with-redux
Terms & Descriptions
The core of everything is the Store, Action, and Dispatch. In its simplest form it’s all you technically need. From there Thunks & Sagas enhance the the tooling around the Dispatching of Actions.
Store
- A singleton object/state for the app.
Action
- A payload describing what data you want to update in the store.
- They have a standardized structure.
- We use this pattern: https://github.com/redux-utilities/flux-standard-action
Dispatch
- You “Dispatch an Action to the Store” to update the store.
- Dispatch sends the Action payload through the Reducer.
Reducer
- Receives the Action payload from Dispatch and modifies the Store.
- Reducers contain no business logic, they only modify the Store described in the Action
Thunk
- Considered the old way, but sometimes still has great applications in simple cases.
- You can “Dispatch a Thunk to either access state or do ajax work”
- Within a thunk you can call additional Dispatch
- Within a thunk you can access the state of the store.
- Good for conditionally firing subsequent api calls, or dispatches.
- Good for pulling together data from the store into a dispatch.
- Good for very simple ajax calls, you can Dispatch Actions from ajax response
- Best way to understand Thunks in my opinion is to look at the 10 lines of source code:
Saga
- Regarded as a better replacement for Thunks
- Can require more effort than Thunks to understand, and build.
- Within a Saga you can access the state of the store.
- Great for conditionally firing subsequent api calls, or dispatches.
- Great for pulling together data from the store into a dispatch.
- Sagas can subscribe to listen and fire when some Actions have been Dispatched
- Great for moving side effects to be self contained instead of sprinkled throughout the app
- Provides great debounce / cancel / takelatest logic nearly for free.
- Can do long running / scheduled Actions
- https://redux-saga.js.org/docs/introduction/BeginnerTutorial.html
(cover photo credit: Jonathan Stassen / JStassen Photography)