How to get multiple cache data in ReactJS ?
Last Updated :
23 Jul, 2024
We can use the following approach in ReactJS to get multiple cache data. We can get multiple cache data from the browser and use them in our application whenever needed. Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when requested.
Prerequisites:
Approach:
To retrieve multiple cache data in React JS, implement a `getMultipleCacheData` function, specifying the cache names, and trigger it on a button click. This example demonstrates fetching data from browser caches named CacheOne and CacheFour out of five available caches: CacheOne, CacheTwo, CacheThree, CacheFour, and CacheFive.
Steps to Create the React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Example: Now write down the following code in the App.js file.
JavaScript
import * as React from 'react';
export default function App() {
// Our state to store fetched cache data
const [cacheData, setCacheData] = React.useState();
// Function to get multiple cache data
const getMultipleCacheData = async (cacheNames) => {
if (typeof caches === 'undefined') return false;
var cacheDataArray = []
for (var i = 0; i < cacheNames.length; i++) {
const cacheStorage = await caches.open(cacheNames[i].cacheName);
const cachedResponse = await cacheStorage.match(cacheNames[i].url);
// If no cache exists
if (!cachedResponse || !cachedResponse.ok) {
cacheDataArray[i] = `Unable to fetch ${cacheNames[i].cacheName}`
} else {
var data = await cachedResponse.json()
cacheDataArray[i] = data
}
}
// Putting commas in between caches data
// to display properly
setCacheData(cacheDataArray.join(', '))
};
// Caches names which has to be fetched from browser
const cachesToFetch = [
{ cacheName: 'CacheOne', url: 'https://localhost:300' },
{ cacheName: 'CacheFour', url: 'https://localhost:300' }
]
return (
<div style={{ height: 500, width: '80%' }}>
<h4>How to get multiple cache data in ReactJS?</h4>
<button onClick={() => getMultipleCacheData(cachesToFetch)} >
Get Multiple Cache Data</button> <br />
<h6>Multiple Cache Data is: {cacheData}</h6>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output:
Now open your browser and go to http://localhost:3000
Similar Reads
How to delete multiple cache data in ReactJS ? In React, we can access all cache data from the browser and modify it as required whenever needed using the Cache Storage of the browser. Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when requested. Prerequisites:NPM & Node.jsReact JSC
2 min read
How to get single cache data in ReactJS ? We can use cache storage using window cache and in React JS to get single cache data. We can get single cache data from the browser and use it in our application whenever needed. Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when requested.
2 min read
How to get complete cache data in ReactJS? In React, we can get all cache data from the browser and use it in our application whenever needed. Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when requested.PrerequisitesReact JSCacheStorageApproachTo get all cache data in React JS defi
2 min read
How To Get Multiple Checkbox Values In ReactJS? Handling checkboxes in ReactJS becomes important when creating forms or managing user input. If you need to select multiple options, we can do it by implementing multiple checkboxes. In this article, we'll explore how to get the values of multiple checkboxes in ReactJS, manage the state, and display
4 min read
How to delete specific cache data in ReactJS ? In React, we can access all cache data from the browser and delete specific cache data from the browser as per the user's requirement whenever needed using the Cache Storage of the browser. Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when
2 min read
How to combine multiple reducers in ReactJS ? To combine multiple reducers in React JS we have a function called combineReducers in the redux. This basically helps to combine multiple reducers into a single unit and use them. Approach In React, to combine and implement multiple reducers combineReducers methods are used. It manages multiple redu
4 min read
How to store single cache data in ReactJS ? Storing Data in a cache is an important task in web applications. We can cache some data into the browser and use it in our application whenever needed. Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when requested. PrerequisitesReact JSCach
2 min read
How to clear complete cache data in ReactJS ? Caching is a technique that helps us to store a copy of a given resource in our browser and serve it back when requested. There is no direct option to clear the cache as it is a client-side feature managed by the browser. To clear the complete cache in React JS we can use JavaScipt's cache storage A
2 min read
How to Fetch Data From an API in ReactJS? ReactJS provides several ways to interact with APIs, allowing you to retrieve data from the server and display it in your application. In this article, weâll walk you through different methods to fetch data from an API in ReactJS, including using the built-in fetch method, axios, and managing the st
8 min read