Redux

Definition

  • Redux is a library for managing and updating application state library which is one-way data flow

  • Redux only have one store which can be an object and be used globally

  • Components will subscribe to the store and the store will notify to its component when changes

  • Reducer is used to take the input and update the store

  • When component want to make change to the store, it needs to dispatch the action which is described the the changes, and reducer take the action as a input and change the store

Reducer

  1. Contain different business logics and return different states based on the actions.

  2. Should be a pure function (not contain other data sources, such as api)

  • Example:

Store

  1. The whole global state of your app is stored in an object tree inside a single store.

  2. Dispatch method is to send the action to reducer to change the state tree

  • Example:

  • Example (dispatch):

  • Example (get the state):

Action

  1. Type is to describe the name of event happened

  2. Content/Payload is to describe the change after event

  3. After dispatching the action, it will pass the argument to the reducer to perform business logic and changed the state tree

  • Example:

Redux Saga

Why Saga?

If we involve the the business logic related to async function (such api call), we need saga to implement it instead of doing it in the reducer, since reducer should be a pure function, also the promise function will be returned instead of the resolve value if the reducer becomes a async function

Flow

Pre-Condition

  • We create rootSaga , apply the saga middleware with reducer to the store and run the rootSaga

RootSaga

  • Divided into 2 parts - worker and watcher

  • Worker: Containing logic to implementing the async function ( use call method) and trigger the existing action ( use put method)

  • Watcher: Monitoring the specific action, if the type of specific action is called, the function of the worker will be triggered, monitoring function mainly contain 2 types : takeLatest and takeEvery. takeLatest: If the previous worker still not finished, the new worker will start and the previous one will be cancelled. takeEvery: even the previous worker is not finished, the new worker can be triggered, and the previous worker is still running at the same time

Redux Toolkit

Why need redux toolkit

  • The action is structured, contains payload and type,

  • createSlice automatically generates a slice reducer with corresponding action creators and action types. so longer need to map the reducer and action by type

  • As the state is immutable, if you need to change the value, you must need to return new object and do the logic on it

  • By using redux-toolkit, since its reducer function already include immer, so you can change the value with user-friendly syntax without mutating the state

Implementation

  • Main (Group the slices together)

  • Slice (Define reducer and action and their relationship)

  • Store

  • Dispatch

  • State

Last updated

Was this helpful?