
Redux is a javascript library for managing application state.
It is most commonly used with libraries such as React.js or Angular, etc.
Redux - Basic Concepts
Suppose, you need to pass data in between such components that don't have any relationship (parent-child), so while making communication between such components, it is difficult to pass data and maintaining them is also a very difficult task.
There are three possible solution to solve this probelm.
- Redux
- Context API
- Hookes - useContext + useReducer
In such a case redux is very useful because Redux eliminates the need to continuously pass data from one component to another component.
If you are using redux with react, states will no longer need to be lifted up
Everything is handled by Redux itself.
Redux provides a way to centralize the state of your application.
Following are the benefits of using Redux:
- Single-Source of Truth
- Predictable States
- Easy to Maintain
- Reusable Code
- No need to uplift State
- Easy to Debug
You may also like - React.js Interview Questions
Redux is available as a package on NPM.
Redux can be described in three basic principles
- Single-Source of Truth
- State is Read-Only
- Changes are made with Pure Functions
The state of your whole application is stored in an object tree, called Store.
The only way to change the state is to dispatch an Action, an object describing what happened.
To specify how the state tree is transformed by actions, you write pure Reducers.
You may also like - JavaScript Interview Questions
Redux allows you to manage the state of the application using single source of truth, called Store, Any Component can directly access the state from the Store using Subscribe method.
Let's understand the Redux workflow step by step:
- Store - Redux offers a solution of storing all your application's state at one place, called store.
- Action - Actions can be dispatch based on the Event (e.g. OnClick, OnChange, etc).
- Reducer - Reducers are the pure functions which takes the previous state and an action, and return the next state. (Always return new state objects, instead of mutating the previous state).
- Subscribe - Any components can easily subscribe to store.
- In Redux, components don't communicate directly with each other, but rather all state changes must go through the single source of truth, the store.
Redux - Advanced Concepts
- Store
- Reducer
- Action
- Action Creator
- Dispatch
- Subscribe
- Provider
- Connect
A Store is an Object which holds the whole state tree of your application.
Reducer are pure fucntions which take the previous state of the app and return a new state based on the action passed to it.
Always remember that we should never mutate state inside the reducer, always return a new copy of state.
You can achieve this using
- Object.assign()
- Spread (…) operator
Actions are plain JavaScript objects and they must have a type property to indicate the type of action to be carried out. They must also have a payload that contains the information that should be worked on by the action.
Actions are dispatch using store.dispatch() method.
Action Creator is a function which return an action.
Dispatch is a method which triggers action with type and payload to Reducer.
Subscribe is a method which is used to subscribe data/state from Store automatically whenever there will any changes in state.
The Provider is a component that has a reference to the Store and provides the data from the Store to the component it wraps.
Connect is a function communicates with the Provider.
Note:
- It is just a basic example (All in One File), that help will help to understand redux implementation.
- In Real Apps, You can organize your code in better ways, easier to maintaine & reuse etc.
A pure function always return same output for the same input arguments.
A pure function is a function which
- always return the same output for the same input
- has no side effects, means it doesn't change any external state.
You may also like - Next.js Interview Questions
You can export the store from the module you called createStore.
You may also like - React.js Interview Questions
Yes
Yes, we can combine multiple reducers using - combineReducers.
No, Redux can be used as a data store. it is most commonly used with React.js and React Native.
Redux can also be used with Angular, Vue.js etc
In Memory, It keeps only reference not actual data.
Middleware are being used in redux for extended functionality. it works between dispatching an action, and the moment it reaches to the reducer.
The most common use case for middleware is to support asynchronous actions.
The Customized middleware functions can be created by writing high order functions (a function that returns another function), which wraps around some logic.
Middleware are used Redux for
- logging
- crash reporting
- routing
- asynchronous API calls
A store enhancer that applies the given middleware.
Redux Thunk is a middleware.
Redux Thunk allows you to write action creators that return a function instead of an action.
The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met.
- redux: main core redux library (independent from react)
- react-redux: connects your redux store with react components
- redux-thunk: a redux middleware which helps you with async actions
- redux-saga: a redux middleware library to handle asynchronous actions
Yes
Without middleware, Redux store only supports synchronous data flow.
Flux is a Design Pattern and Redux is a library
REDUX | FLUX |
---|---|
Single Store | Multiple Store |
Redux is a container for JavaScript apps. | Flux is a container for application state and logic that are registered to callbacks. |
It offers interesting features such as writing applications, testing in different environments such as a server, client, etc. | It is an observer pattern that is modified to fit React. |
In redux, actions can be functions or promises. | It is a fancy name given to observer pattern and Facebook has developed tools to aid the implementation of these patterns. |
Redux is the first choice for web developers because it offers live code editing. | Flux supports actions that are simple JavaScript objects. |