Documenting my learning journey of Namaste React Live Course conducted by Akshay Saini
Both usecontext and redux are used to solve props drilling
, a problem faced while passing props between components.
Context API | Redux |
---|---|
Context provides a way to share values between components (throughout the application) without having to explicitly pass a prop through every level of the tree. |
Redux is a central store for storing the data of the applications. |
Context API is built-in React tool and does not have to be downloaded separately |
Redux is an third-party open source library not part of React which provides a central store , and actions to modify the store. |
Requires minimal Setup | Requires extensive setup to integrate it with a React Application |
Specifically designed for static data, that is not often refreshed or updated | Usefule for both static and dynamic data |
Difficult to debug | Easy to debug using Redux dev tool |
Useful for small projects | Useful for larger projects |
-
Abstraction and Convenience:
Redux Toolkit provides a set of abstractions and conveniences on top of regular Redux, which make it easier to work with and manage the state of your application. This includes features such as thecreateSlice
function for creating slices of state and its associatedactions and reducer
, and thecreateStore
function for creating aRedux store
with pre-configured middleware and enhancers. -
Immutable updates:
Regular Redux requires you to create anew state object
every time you make an update, which can become repetitive and error-prone. Redux Toolkit provides a way toupdate the state immutably
, using its built-increateSlice
function. -
Simplified Reducers:
In regular Redux, you write yourown reducers
, which can become complex and difficult to manage as your application grows. With Redux Toolkit, you can use thecreateSlice
function to generate reducers for you, based on the state updates you define. -
Improved Performance:
Redux Toolkit uses advanced performance optimizations, such as memoization, lazy evaluation, and selective updates, to make your application faster and more efficient. -
Better Debugging:
Redux Toolkit provides better debugging tools, such as the ability to log and replay actions, inspect the current state of your application, and easily track the changes made to your state over time.
A dispatcher is a function
that dispatches actions to the store. In Redux, actions are used to describe changes to the state, and dispatching an action is the way to trigger those changes.
- How to create & use dispatcher function ?
const dispatch = useDispatch();
This hook returns a reference to the dispatch function
from the Redux
store. You may use it to dispatch actions as needed.
dispatch(actionCreator(data)); // returns an action payload object
When you dispatch an action creator, it returns an action object
that the reducer function
uses to update the state
. The dispatcher function is used to dispatch the action creator and which in turns calls the reducer function to trigger the update.
A reducer is a pure function
in Redux that takes the current state
of your application and an action
, and returns a new state
based on that action
.
Example :
addItem: (state, action) => {
const item = state.items[action.payload.id];
const quantity = item && item.hasOwnProperty('quantity')
? state.items[action.payload.id]?.quantity + 1 : 1;
state.items[action.payload.id] = { ...action.payload, quantity };
state.totalItemsCount = state.totalItemsCount + 1;
},
Here based on the action object, the state is updated inside teh reducer function.
In Redux Toolkit, a slice
is a piece of the state that is managed by a single set of actions and reducer.
A selector
is a pure function
that takes the current state
of your application and returns a derived value based on that state.
useSelector
is a hook from the react-redux
library that allows you to subscribe
to the state
of your Redux store from a React component. The useSelector
hook takes a selector function
as its argument, which is used to extract data from the state tree. The component will re-render whenever the state of your Redux store changes and the derived value returned by the selector function changes.
const totalItemsCount = useSelector(store => store.cart.totalItemsCount);
store => store.cart.totalItemsCount is the selector function which returns the totalItemsCount from the state. Now, useSelector() is used to subscribe to this totalItemsCount from the state.
The createSlice
function is used to create a store slice, a piece of the store.
The createSlice
function takes an object
as an argument, which contains the following properties:
name:
A string that represents the name of the slice.initialState:
Anobject
that represents theinitial state
of the slice. In our cartSlice example, the initial state is an object with two properties:items
(an empty object) andtotalItemsCount
(which is 0).reducers:
Anobject
that contains the Redux reducers for the slice. Reducers are functions that take the currentstate
and anaction
, and return a new state based on the action type and payload. In our example, there are three reducers: addItem, removeItem, and clearCart.
After creating the slice, the code exports
the actions
that can be dispatched on the store. In this example, there are three actions: addItem, removeItem, and clearCart.
Finally, the code exports
the reducer
for the slice using the reducer property of the slice. The reducer is responsible for managing the state of the slice and updating it in response to dispatched actions.