DEV Community

Hihi
Hihi

Posted on

My quick note on Redux

Redux is a state management library that helps us to solve the problem of state usage from many places in app and passing props to a deep nesting component.

Redux ensures 3 pillars in its pattern: 1-way binding data & single source of truth & immutability;

Everything in Redux is communication with actions. Action is an object that describe what to do and return the new state from reducer

Actions Creator is a function returning a specific action object => prevents wrong typing when typing action objects again and again.

Reducer: A pure function takes in the current state object and a callback function which defines logic and returns new states; the idea of reducer comes from Array.reduce method.

Redux toolkit: compact action creators, reducer, and selectors into a slice - opinionated architecture.

We can create multiple stores in redux but it will violate the single source of truth.

thunk: a middleware that listens to dispatched events and sees if the arguments passed is a function or not. If it's a function then it will be executed (asynchronously)

> Redux with normal action:
UI dispatch ---> Actions ---> Reducer receive action and return new state;

> Redux with thunk (passing a function to dispatch instead of object):
UI dispatch ---> thunk ---> middleware executes thunk ---> dispatch action to reducer ---> reducer returns new state.

> Redux with thunk (passing a function to dispatch instead of object):
UI dispatch ---> thunk ---> middleware executes thunk ---> dispatch action to reducer ---> reducer returns new state.
Enter fullscreen mode Exit fullscreen mode

Ref:

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay