Introduction to Redux – a simple practical Redux example

In this tutorial, we’re gonna introduce main concept of Redux: what it is, how to work with Redux Store, Action, Reducers. Then we will practice to understand all of them in a simple practical Redux example.

Overview

Redux

Redux is a state container that helps JavaScript applications to manage state.
=> Whenever we wanna read the state, look into only one single place – Redux Store.
=> Managing the state could be simplified by dealing with simple objects and pure functions.

Redux Store

Store holds the current state so that we can see it as a single source of truth for our data.
With Redux Store, we can:
– access to state via store.getState()
– update state via store.dispatch(action)
– register listeners via store.subscribe(listener)
– unregister listeners via the function returned by store.subscribe(listener):

*Note: We only have a single store in a Redux application.
=> When we wanna split data handling logic, we combine Reducers instead of using many Stores.

Redux Reducer

Reducer is a pure function that generates a new state based on an Action it receives. These Actions only describe what happened, but don’t describe how state changes.

*Note: Reducer must be a pure function:
=> From given arguments, just calculate the next state and return it.
=> No side effects. No API or non-pure function calls. No mutations.

Redux Action

Action is payload of information that is sent to Store using store.dispatch(action).
Action must have a type property that should typically be defined as string constants. It indicates the type of action being performed:

Sometimes we may wanna move Actions into a separate module (for example, in case our app is getting larger).
We also have functions that return Redux Actions:

Practice

Example overview

This is a simple Redux Application that has:
count as the main state that is stored inside Redux Store.
– 4 types of Action: 'ADD', 'MINUS', 'SET', 'RESET'.
– Reducer with initial state: count = 0.

We can add/minus a number to count, set or reset count value.

Step by step
Install Redux

Add Redux to dependencies in package.json:

Then run cmd yarn install.

Create Redux Actions

We have 4 Action creators, each creator simply returns object which contains type and payload.

Create Redux Reducer

Reducer has switch statement that chooses what to do to return new state based on action’s type and payload:

Create Redux Store

Just import the createStore function from Redux, then pass it to Reducer:

Subscribe & Unsubscribe

listener (inside subscribe() function) will be invoked any time we call store.dispatch(), we can call store.getState() to read the current state inside the callback.

Calling unsubscribe() is stopping listening to state updates.

Run and Check result

Full code:

Result in Browser Console:

redux-practical-example-result

Source code

Redux-Intro

For running:
yarn install
yarn run dev-server or yarn run build, then yarn run serve.



By grokonez | April 12, 2018.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*