What is React Redux? | Advantage of Redux
Introduction
Much the same as React, Redux is likewise a library which is generally used for front-end. It is a fundamental tool for overseeing both data state and UI-state in JavaScript applications. Redux divides the application information and business logic into its compartment to let React oversee only the view. Instead of a customary library or a structure, it's an application information stream engineering. It is generally perfect with Single Page Applications (SPAs) where the administration of the states after some time can get intricate.
Redux was made by Dan Abramov and Andrew Clark around June 2015. It was enlivened by Facebook's Flux and impacted by programming language Elm.
To use React-Redux with React application, you need to install it by running the command on the prompt i.e. npm install redux react-redux –save
Why React Redux?
React flows in unidirectional that means the data always be in a proper manner that makes the application more efficient. So that the communication between the components is easy. But what if the component tries to communicate to the non-parent component and how two pass the data to each other.
Redux gives a "store" as an answer to this issue. A store is where you can store all your application state together. Presently the parts can "dispatch" state changes to the store and not straightforwardly to different components. At that point, the components that need the updates about the state changes can "subscribe" to the store.
In this manner, with Redux, it turns out to be clear where the parts get their state from just as where should they send their states to. Presently the components starting the change don't need to stress over the rundown of components requiring the state change and can just dispatch the change to the store. This is how Redux makes the information stream simpler.
Components of Redux
There are four major components of Redux:
- Action
- Reducer
- Store
- View
Action:- Action is the objects in the JavaScript that help us to send the data from the application to the store. Action is only responsible for stores to receive information. Using store.dispatch() for sending the data to the store. Eg:
function addTodo(text) {
return {
type: ADD_TODO,
text
}
}
dispatch(addTodo(text));
Reducer:- Reducer specify how the application’s state changes in response. It depends on the exhibit decrease technique, where it acknowledges a callback (reducer) and lets you get a single value out of various qualities, entireties of numbers, or a gathering of floods of esteems. In Redux, reducers are functions that take the present status of the application and activity and return another state. Seeing how reducers work is significant because they perform the majority of the work. Eg:
function reducer(state = initialState, action) {
switch (action.type) {
case ADD_TODO:
return Object.assign({}, state,
{ todos: [ ...state.todos,
{
text: action.text,
completed: false
}
]
})
default:
return state
}
}
Store:- A store is a JavaScript object which can hold the application's state and give a couple of techniques to get to the state, dispatch activities, and register audience members. The whole state/object tree of an application is spared in a solitary store. Therefore, Redux is exceptionally straightforward and unsurprising. We can pass middleware to the store to deal with preparing information just as to keep a log of different activities that change the condition of stores. All the activities return to another state using reducers. Eg:
import { createStore } from 'redux'
import todoApp from './reducers'
let store = createStore(reducer);
View:- The main reason behind the view is to show the information went somewhere near the store. The components are responsible for the activities. The stupid components underneath the keen component tell them on the off chance that they have to trigger the activity. The shrewd segments, thus, go down the props which the stupid component treats as callback activities.
Advantage of Redux
Some of the major advantages of Redux:
- Consistency of result – Since there is consistently one wellspring of truth, for example, the store, there is no disarray about how to match up the present status with activities and different pieces of the application.
- Viability – The code gets simpler to keep up with an anticipated result and severe structure.
- Server-side rendering – You simply need to pass the store that is made on the server, to the customer side. This is exceptionally helpful for beginning render and gives a superior client experience as it improves the application execution.
- Engineer apparatuses – From activities to state changes, designers can follow everything going on in the application continuously.
- Network and environment – Redux have an immense network behind it which makes it significantly all the more enamoring to utilize. An enormous network of skilled people add to the advancement of the library and create different applications with it.
- The simplicity of testing – Redux code is generally works which are little, unadulterated, and separated. This makes the code testable and free.
- Association – Redux is exceptionally exact about how the code ought to be composed, this makes the code increasingly reliable and simpler when a group works with it.