Redux - as simple as it can get
Tags: redux, javascript, beginners, react
Published: 2019-09-16 11:02:15

<a href="https://www.gistia.com/beginners-guide-redux/"><i>Redux<i/></a>, by it's definition, is a state management framework that can be used with different web technologies, including react and ruby. <i>Redux is not part of react!</i>

It is efficient when you are working on a large application that requires changing states in different components. Assuming you are working on an application that has over 10 components, and each component has its own local state, the entire state of the application can be managed on a single object housed in Redux <a href="https://redux.js.org/basics/store#store">store</a>.

I am going to share some basic steps I use to set up redux.

<b>Step 1: Creating your Initial State</b>

A state in simple terms, is a JavaScript object that stores dynamic data that will be used in the components. This data determines the component's behaviour. Assuming Student represents your application's current state in time. Let's define our student current state:-
```javascript 
const initialState = {
  name: 'Valerie',
  grade: 4,
  age: 8
};
```

<b>Step 2: Defining your Action(s) and Action Creator(s)</b>

An <a href="https://redux.js.org/basics/actions#actions">Action</a> is simply a JavaScript object that contains information that send data from your application to your store. They are the only source of information for the store.
In our example, our actions can be to enroll a student, pay their fees or assign them a dormitory. Let's define an action. Defining a Redux action is as simple as declaring an object with a type property. We first declare our action type by assigning it to a constant variable. This is just best practice.
```javascript
const ENROLL = 'ENROLL';
```

Next, we define our action creator which will be used to send the action to the store. An action creator is simply a JavaScript function that returns an action. 
```javacript
const enrollStudentAction = (status) => {
  return {
    type: ENROLL,
    status: status
  }
};
```

<b>Step 3: Creating your reducer(s)</b>

A <a href="https://redux.js.org/basics/reducers#reducers">Reducer</a> describes how our state changes in relation to the <a href="https://redux.js.org/basics/actions#actions">actions</a>that we have described. A reducer is a function that takes in the state as the first argument and action. Reducers in Redux are responsible for the state modifications. Reducers return a new state.

```javascript
const studentReducer = (state = initialState, action) => {
  switch (action.type) {
    case ENROLL:
      let newInitialState = Object.assign({},state);
       newInitialState.status= action.message;
        return newInitialState;
    default:
      return state;
  }
}
```
<b>N/B</b>
In Redux, state is read-only i.e, the reducer function must always return a new copy of state and never modify state directly. Redux does not enforce state immutability, however, you are responsible for enforcing it in the code of your reducer functions.

In our case, our state is an object. In order to enforce state immutability in objects, we use ``Object.assign()`` utility. ``Object.assign()`` takes a target object and source objects and maps properties from the source objects to the target object. Any matching properties are overwritten by properties in the source objects. 

So our ``Object.assign({},state)`` utility takes in an empty object as the target object and our state which is our ``initialState`` object as the source. This just creates a clone of our state. Now that we have our state object clone, we can then add a new property in it. Our new property is status and is set to a value 'enrolled'.

<b>Step 4: Creating our store</b>

As I mentioned above, the <a href="https://redux.js.org/basics/store#store">store</a> houses our state and all the logic to update it are passed to it. To create our store, we use a ``createStore()`` function from `redux`. It takes the reducer, state - or if you like to call it `preloaded state` and in some cases enhancers e.g. applymiddleware(). But I won't go in to that. I want to this  as minimal as possible.

```
const store = Redux.createStore(studentReducer, initialState)
```
There we have our store ready!

<b>State 6: Subscribe</b>

<a href="https://redux.js.org/api/store#subscribelistener">Subscribe</a> just lets you know every time the store changes. If you're writing a <a href="https://react-redux.js.org/5.x/introduction/basic-tutorial#basic-tutorial">react-redux</a> application then you never need it because react-redux does this for you. (i.e. essentially it watches for changes and then tells react to redraw). For example in our case, if we want to know whether the our student state is changing, we can add an event listener. It will be called any time an action is dispatched, and some part of the state tree may potentially have changed. You may then call getState() to read the current state tree inside the callback. For example, in our case, if we want to check whether a new student is enrolled or not, we can create an event listener called ``handleChange`` like this:-
```javascript
function handleChange() {
  const currentStudent = store.getState();
  if (currentStudent.status === 'enrolled') {
    console.log('A new Student is enrolled!');
  } else {
    console.log('Student not yet enrolled!');
  }
}
```
then we ``subscribe`` this event to the store like this:-
```javascript
store.subcribe(handleChange)
```


<b>Finally Dispatching our actions</a>

Dispatching simply means, passing the logic (actions) that we have created to the redux store. This is the moment of truth now. We can now test our update logic.
Dispatch method is what you use to dispatch actions to the Redux store. Calling ``store.dispatch()`` and passing the value returned from an action creator sends an action back to the store.

```javascript
store.dispatch(enrollStudentAction('Enrolled'));
```

We can check that our state changes by calling ``getState()`` method on the store:-

```
// Log the initial state
console.log(store.getState()
```

Once you do this, you will see that our state now has ``status: 'enrolled'``` property at the bottom. We killed it!

Cheers!