We can use the following approach in ReactJS to get all cache data. 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 stores a copy of a given resource into our browser and serves it back when requested.
Approach: Follow these simple steps in order to get all cache data in ReactJS. We have created our getAllCacheData function which gets all data from the browser cache. When we click on the button, the function is triggered and data gets fetched from the cache. In the following example, we are trying to fetch all cache data from the browser which has five caches named CacheOne, CacheTwo, CacheThree, CacheFour, and CacheFive as shown below:
Creating React Application:
- 
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: It will look like the following.
 
Project Structure
App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
Javascript
| import * as React from 'react';  Âexport defaultfunctionApp() {  Â  // Our state to store fetched cache data   const [cacheData, setCacheData] = React.useState();  Â  // Function to get all cache data   const getAllCacheData = async () => {  Â    // List of all caches present in browser     varnames = await caches.keys()  Â    varcacheDataArray = []  Â    // Iterating over the list of caches     names.forEach(async(name) => {  Â      // Opening that particular cache       const cacheStorage = await caches.open(name);  Â      // Fetching that particular cache data       const cachedResponse = await cacheStorage.match(url);       vardata = await cachedResponse.json()  Â      // Pushing fetched data into our cacheDataArray       cacheDataArray.push(data)       setCacheData(cacheDataArray.join(', '))     })   };  Â  return(     <div style={{ height: 500, width: '80%'}}>       <h4>How to get all cache data inReactJS?</h4>       <button onClick={() => getAllCacheData()} >         Get All Cache Data</button>  <br />       <h6>All 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/, you will see the following output:


 
                                    







