![]() |
For a smooth user experience, fast rendering in React applications is crucial. Using React-Redux can sometimes lead to performance issues due to unnecessary re-renders of connected components. This article will explore several methods to avoid the rendering of these components. By implementing these techniques, you can enhance the efficiency and performance of your React-Redux applications, ensuring they run smoothly and responsively. These are the following approaches to optimize the rendering of connected components in React-Redux: Table of Content Memoization with ‘React.memo’‘React.memo’ is a HOC(high-order component) used to memoize the functional components so that re-render may be blocked for them if props are same. Using ‘React.memo’, you can make sure that a functional component is rendered only when its props change. This lowers the rendering overhead for components with no changed props. Syntax:const MemoizedComponent = React.memo(MyComponent); Using ‘useSelector’ and ‘useDispatch’ HooksThe ‘useSelector’ hook helps you extracts data from the Redux store, whereas ‘useDispatch’ allows you to dispatch actions. This can enable to subscribe only when parts of the state changes, so that unnecessary re-renders are minimized. Syntax:import { useSelector, useDispatch } from 'react-redux'; Splitting State SlicesDividing the state into smaller, more manageable views can improve performance by decreasing the number of components that need to re-render whenever a portion of the state is changed. By splitting the state, you can separate the state changes impact such that only the necessary components are re-render. Syntax:import { configureStore } from '@reduxjs/toolkit'; Selector Optimization with ‘reselect’The ‘reselect’ library is useful for creating memoized selectors that recalculate only when the values of the inputs change. Memoized selectors minimize unnecessary computations done thus speeding up component rendering. Syntax:import { createSelector } form 'reselect'; Avoiding Anonymous Functions and objects in PropsWhen we pass anonymous functions or objects to other components using props in React, this can potentially lead to some unnecessary re-renders because each new reference is treated as a different entity each time it changes its host component’s state. This is not so with named functions and constants whose values remain the same even after their references are updated in other parts of code. Syntax:// Avoid this Step to Optimize the re-render in react-redux connected componentStep 1: Create React Appnpx create-react-app redux-performance Step 2: Install Required Modulesnpm install @reduxjs/toolkit react-redux reselect Project Structure:![]() Folder Structure Dependencies:dependencies: { Example: Set up a Redux store, create actions and reducers for ‘prop1’ and ‘prop2’, connect a memoized component to display them, and wrap it in the Redux provider in the main app.
Output: Below is a demonstration showing the initial rendered ‘MyComponent’ displaying ‘prop1’ and ‘prop2’ values from the Redux store, and how the component re-renders when these values are changed. If the values of ‘prop1’ and ‘prop2’ in the Redux store do not change, ‘MyComponent’ will not re-render, demonstrating the efficiency of using ‘React.memo’. |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |