by Bagas Dimas Permadi, Programmer at Mitrais
What is Redux?
Several important qualities of Flux have inspired the creation of Redux. Similar to Flux, Redux recommends that you focus your model update logic in a specific layer of your application (“stores” in Flux, “reducers” in Redux). In this case, both Redux and Flux instruct you to define every mutation as a plain object called an “action”.
Why use Redux?
- It’s simpler to learn than Facebook’s FLUX singleton approach
- Compared with other solutions, Redux is more powerful.
- It also offers many features which are unique and very useful in real life web apps development (like immutability).
- It’s the most used solution within the React community. Redux is the standard tool for use in uni-directional data flows.
- Server side rendering with Redux is great. As a server-side for React applications, Redux is still one of the best. This feature is not only unique, but it is also useful. It is mostly used on websites where the user experience must be first-class (the lower latency of the server’s response, the better). For example: AirBnb and the most popular news services have server rendering implemented, so that their users get content served much quicker than in a regular (non server-side rendering) single-page-application.
Redux can be described in 5 ideas:
- All of your application’s data is in a single data structure called the state which is held in the store.
- Your app reads the state from this store.
- Mutation of the state never happens outside the store.
- The views emit actions that describe what happened.
- A new state is formed by linking the old state and the action by a function called the reducer.
Redux can be narrowed down into the following:
- The store manages the states and usually there is a method to dispatch an action. In a Redux app, you can acquire it’s states through store.getState()
- The reducer decides how to alternate a state after receiving an action. Therefore it can be seen as the entrance to a state change. A reducer is also comprised of functions, and it alternates states by perceiving an action as an argument, which then returns a new state.
- The middleware is the middleman between a store.dispatch() and a reducer. The purpose of the middleware is to intercept an action that has been dispatched, and modify or even cancel the action before the reducer receives it. For the example, if we want to log some state value before and after the action, we don’t need to log the action and the next state every time you call store.dispatch(action). We can use middleware to intercept that process and log the value before and after an action has been dispatched.
Logging manually (without middleware)
Logging using middleware
How Redux works with React
React data flow is called “unidirectional data flow”, which indicates that data flows in one direction from parent to child. In this case, it’s not clear how two non-parent-child components would communicate in React:
Pict 1. React’s data flow without Redux
This approach is regarded as poor practice by many, because direct component-to-component communication is error prone and can lead to spaghetti code.
This is where Redux is very helpful. Redux provides the solution of storing all your application state in one place, called a “store”. Afterwards, components “dispatch” state changes to the store, instead of directly to other components. The components that need to look out for state changes can “subscribe” to the store:
Pict 2. React’s data flow with Redux
With Redux, components don’t communicate directly with one another, but rather all state changes must go through the single source of truth – the store. All components receive their state from the store. It’s also clear that components should send their state changes to the store as well. The component initiating the change is only involved with dispatching the change to the store, and does not interact with other components that need the state change. This is how Redux makes data flow easier to discuss.