How to Normalize State in Redux Applications ?
Last Updated :
03 Apr, 2024
In Redux applications, efficient state management is essential for scalability and maintainability. Normalization is a technique used to restructure complex state data into a more organized format, improving performance and simplifying state manipulation.
This article covers the concept of normalization in Redux, exploring various approaches, steps to implement it, and an example application.
Approach 1: Manual Normalization
- Manually restructuring nested data structures into separate entities.
- Define unique identifiers for each entity.
- Normalize relationships by storing references or foreign keys.
Approach 2: Using Normalizr Library
- Utilize the Normalizr library to streamline the normalization process.
- Define schemas to specify how nested data should be normalized.
- Use normalize() function provided by Normalizr to normalize data based on defined schemas.
Normalizing State in Redux Applications
- Identify Entities: Break down your application data into distinct entities. These entities could represent different types of objects such as users, posts, comments, etc.
- Normalize Data: Normalize each entity's data structure. This means organizing related data into separate tables/objects and establishing relationships using unique identifiers (IDs).
- Flatten Structure: Flatten nested data structures to simplify access and updates. Instead of embedding objects within other objects, store references to them using their IDs.
- Create Lookup Tables: Maintain lookup tables or indexes to quickly access specific data by ID. These tables could be objects or maps where keys are IDs and values are the corresponding entity objects.
- Use Selectors: Implement selectors to encapsulate the logic for accessing normalized state. Selectors abstract away the details of state shape, making it easier to retrieve data from the store.
Steps to Create React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app myapp
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd myapp
Step 3: Install Redux and Normalizr packages:
npm install redux normalizr
Folder Strutcure:
Project StructureUpdated dependencies in package.json file:
"dependencies": {
"normalizr": "^3.6.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: Below is an example of normalize state in Redux Applications.
JavaScript
import React from 'react'
import Normalize from './components/Normalize'
const App = () => {
return (
<div>
<Normalize />
</div>
)
}
export default App
JavaScript
import React from 'react'
import { normalize, schema } from 'normalizr';
const Normalize = () => {
const userSchema = new schema.Entity('users');
const postSchema = new schema.Entity('posts', {
author: userSchema
});
const data = {
users: [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
],
posts: [
{
id: 1, title: 'Post 1',
author: { id: 1, name: 'Alice' }
},
{
id: 2, title: 'Post 2',
author: { id: 2, name: 'Bob' }
}
]
};
const normalizedData = normalize(data, {
users: [userSchema],
posts: [postSchema]
});
console.log(normalizedData);
return (
<div style={{
textAlign: "center",
fontWeight: "bolder",
fontSize: "larger"
}}>Normalize State in Redux Applications</div>
)
}
export default Normalize
Output:
Output
Similar Reads
How to manage global state in a React application? Global state refers to data that is accessible across multiple components in a React application. Unlike the local state, which is confined to a single component, the global state can be accessed and modified from anywhere in the component tree. In this article, we will explore the following approac
7 min read
How do you handle real-time updates in Redux applications? Handling real-time updates is essential for modern web applications to provide users with dynamic and interactive experiences. In Redux applications, managing real-time updates efficiently involves implementing strategies to synchronize the application state with changes occurring on the server in r
5 min read
How to Optimize the Performance of React-Redux Applications? Optimizing the performance of React-Redux applications involves several strategies to improve loading speed, reduce unnecessary re-renders, and enhance user experience. In this article, we implement some optimization techniques, that can significantly improve the performance of applications. We will
9 min read
How To Implement Optimistic Updates in Redux Applications? Optimistic updates enhance user experience by making your Redux application feel more responsive. This technique involves updating the UI before receiving confirmation from the server, assuming that the update will succeed. If the server responds with an error, the application can then revert to the
4 min read
How to Create Store in React Redux ? React Redux is a JavaScript library that is used to create and maintain state in React Applications efficiently. Here React Redux solves the problem by creating a redux store that stores the state and provides methods to use the state inside any component directly or to manipulate the state in a def
4 min read