Redux?! How does this work?!
Published: 2020-09-28 22:41:20

About two months ago I started my journey with JavaScript and React. I think every developer goes through the thought processes of, <i>"Oh! React is a great framework! I can see how this can be helpful"</i>. Then comes Redux, a library that makes you doubt everything you think you have learned. Until one day you have an epiphany and EVERYTHING makes sense in your head and you finally understand that Redux makes things even easier at the end. That has been my process with React-Redux. A total roller coaster. But what is Redux?

Redux is just a JavaScript library. The most important thing to know is that Redux is pure JavaScript, so it is agnostic to the framework and can be used with any framework library such as React or Angular. 

Redux is based on this main concepts:

<strong>A. A single <i>"source of truth"</i>:</strong>
Redux uses a single Store to storage our state. The entire state is stored in a tree. If you think about it, in JavaScript this would be achieved using a JavaScript object. If you want, and it helps you understand it, you can think that this is what is happening behind the scenes.

<strong>B. The status is read-only:</strong>
We cannot modify the state directly, we can only read from it to represent it in the view and if we want to modify it, we have to do it through actions. But, what is an action? An action is simply a JavaScript object that includes at least one attribute ("type") that indicates the type of action we are issuing and in case there is data associated with the change or modification, an attribute ("payload") whit that data. Sounds complicated but it's not. I promise. 

<i> Example of an Action ⬇</i>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g2iwp0e3knopyrtxnbiz.png)

<strong>C. Changes with pure functions: </strong>
Since we cannot modify the state directly (just through actions) and the state is stored in a single Store, to specify how to make changes in the state tree we use pure functions called reducers <i>"(a pure function is simply a function that returns the same result to the same input data)"</i>. The reducer is just that, a function that receives two parameters, the initial state, and an action. Depending on the type of action it will perform one operation or another on the state. Always immutable, <strong>WE CANNOT MODIFY THE STATE</strong>, if not create a copy from the previous one. This makes it easier to track down possible errors. 

<i> Example of a Reducer ⬇</i>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/urd1g251j3j6pagtcqmb.png)

Redux is much more than this, but these are its basic principles. The sooner you understand how your actions and reducers work the closer you will be to being able to use such a powerful library as Redux. What Redux does in my own words is to create a maintainable application with a great architecture. You can choose to don't use it but the architecture of the application you are making will not be maintainable and if it grows it will be more difficult to make changes. That is why Redux dictates the guidelines for a good architecture, because it helps to do things right from the start.

Thank you for reading! 😃