Redux: Town of Magical Order

conjurer - Sep 15 - - Dev Community

An interesting approach!

The Magical Town of "Redux" 🏰

This town had one amazing feature: no matter what happened, everyone in town always knew the exact state of everything going on. No confusion, no chaos—everything was magically managed! How?!

Let's see.

The Requests (Actions) ✉️

If someone wants something to happen in the town, they fill out a request form called an Action.

An Action is a simple document that says:

  1. What you want to do (Adopt a dragon).
  2. Any extra details (Adopt a fire-breathing dragon).

Example actions:

  1. Action Type: "ADOPT_DRAGON"
  2. Details: Fire-breathing dragon

These requests are super important because they let the town make changes in a predictable way. No one can just go rogue!

The Messengers (Dispatch) 🏃‍♂️

Now how do people submit their requests? They use messengers. The townspeople call this process Dispatching an Action.

When a resident wants to make a change, they dispatch an Action (request) to the Mayor.

The Mayor (Store) 👨‍💼

In Redux, there’s a Mayor who keeps track of everything happening in the town. He knows:

  • How many houses are built.
  • What’s in everyone’s fridge.
  • How many unicorns are running around!

The Magical Scroll (State) 📜

This scroll holds the entire truth of the town. The scroll updates whenever something official happens, and every resident trusts the scroll.

A reference:
reference

The Wizards (Reducers) 🧙‍♂️

Now, who handles these dispatched requests? The Wizards in town! These Wizards are known as Reducers.

Whenever the Mayor receives an Action (request), he sends it over to the Reducers, who are magical beings. Their job is to take the current State (the magical scroll) and the Action, then figure out what the new State should be.

For example:
If the Action is to adopt a dragon, the Wizard makes sure there’s room for a dragon and updates the scroll.

Rule: Wizards can’t change the scroll directly. They always make a copy of the current scroll, make the necessary changes on the copy, and send that back to the Mayor.

The Town Criers (Subscribers) 📢

Finally, Redux has this special group. These are creatures who like to stay updated with everything that happens. Whenever the State (magical scroll) changes, the Town Criers immediately announce the new State to everyone who cares to listen.

Why Is Redux Awesome? 🎩

  • Single Source of Truth: Everything about the town (the State) is written down in one place (the magical scroll).
  • Predictability: The Reducers handle all changes in the town in a controlled, predictable way.
  • Structure: No random changes, no chaos. Everything is handled through Actions, which ensures order.
. . . . . . . . .
Terabox Video Player