Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Add Large Amounts of Data in the App State in Redux

Redux stores can store unlimited amounts of data usable across multiple components. This guide covers adding large amounts of data in a React Redux app state.

Oct 16, 2020 • 6 Minute Read

Introduction

Redux stores are global states that store data you want to use across multiple components without drilling props at every level in your component tree. As an app gets large and complex, you might want to store large, bulky data inside your Redux store and access it inside a component. A Redux store doesn't have a limit on the amount of data stored, so you can pretty much use it to store almost anything, including bulky JSON data, data in table form, etc. This guide will demonstrate how to add large amounts of data in your React Redux app state.

Creating Redux Boilerplate

In an empty Create-React-App React-Redux project, set up a simple store and reducer using the below code in a file called store.js inside the root directory.

      import { createStore} from 'redux';
const initailState={
    data:'lorem ipsum',
}
const rootReducer=(state=initailState,action)=>{
    switch(action.type){
        case 'ADD_DATA':
            return{
                ...state,
                data: action.payload
            }
        default:
            return state;
    }
}

export default createStore(rootReducer)
    

Next, wrap your app around a Provider component and pass the store as props so that all the components inside this Provider component can use the store inside index.js.

      import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import {Provider} from 'react-redux';
import store from './store';

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

serviceWorker.unregister();
    

The store will initially have some short and simple data. Next, create a file action.js and create an action creator with an action of type ADD_DATA. It also contains an object addData that connects the action type and payload properties to the data passed through dispatch() method.

      const data='new data';
export const ADD_DATA = "ADD_DATA";
export const addData = {
      type: ADD_DATA,
      payload: data
};
export default addData;
    

Finally, connect the store and dispatch() to a component that can dispatch some action and append data to the global state.

      import React,{useState} from 'react';
import {connect} from 'react-redux'
import {addData} from './action';
import './App.css';

function App({addData,data}) {

  return (
    <div className="App">
      <h1>App</h1>
      <button onClick={addData}>Add Data</button>
    </div>
  );
}

const mapStateToProps=(state)=>({ data:state.data })

const mapDispatchToProps = (dispatch) => {
  return {
    addData: ()=> dispatch(addData)
  }
}
export default connect(mapStateToProps,mapDispatchToProps)(App);
    

The app component renders a simple button that fires or dispatches the ADD_DATA action. On dispatching this action, you add some simple data to your global store's state.

Creating Large JSON Data

The next step is to create a data.js file where the dummy data can be put for testing. Create a new file called data.js inside the root directory and add some large amounts of data inside it. The example uses a large JSON data of 1,000 entries created using online mock data generating tool Mockaroo. You can use the same tool to generate any kind of large data or use your own data. Inside the data.js file, copy and paste the large JSON data and export it as an object.

      export default [
	{
      "id": 1,
      "first_name": "Zackariah",
      "last_name": "Rembaud",
      "email": "[email protected]",
      "gender": "Male",
      "ip_address": "40.23.171.141"
    },
    ...
]
    

This data can be used anywhere throughout your app by importing it like a regular object using the import keyword at the top of your component.

      import data from './data.js';
    

Adding Large Amounts of Data in State

Import the large data inside the action.js file and pass this data to the payload property inside the addData object.

      import data from './data.js';

export const ADD_DATA = "ADD_DATA";
export const addData = {
      type: ADD_DATA,
      payload: data
};
export default addData;
    

You are now ready to dispatch the ADD_DATA action from your app component and add large JSON data of thousands of entries to your app's global store's state. Click the Add Data button and you can check that your store's state contains this large JSON data inside it. Thus you can add any amount of data in your app state by dispatching an action that stores that data in your Redux store through a reducer.

Analyzing Large Write Operation

Storing a single line of data or a thousand entries of a complex JSON object makes no difference to Redux, as your Redux store is independent of the amount of data stored in it. On clicking the Add Data button, the ADD_DATA action is dispatched. This adds the large data inside the store without any flickering or UI lag, indicating that there is no performance difference when you add large data in the app state. You can try out with an even larger data set but the results will be the same.

For a more detailed analysis, you can use Google Chrome's Lighthouse to evaluate your app's performance by generating an app report after performing this operation, in one case using simple data and in another using the large JSON data, and compare the performance reports of both. You will find that both show nearly the same performance, showing that adding large amounts of data doesn't affect your app's performance.

Conclusion

Your Redux store imposes no limitations on the size of the data stored. Dispatching an action that performs this operation will not downgrade the performance of your app, so you can safely store any amount of data in your app's state. However, if you're storing large data in the app state, at some point you may want to show it to the user by rendering it on the DOM. You may want to use techniques like lazy loading to improve your app's performance in such situations while still delivering all the content on your app.

Gaurav Singhal

Gaurav S.

Guarav is a Data Scientist with a strong background in computer science and mathematics. He has extensive research experience in data structures, statistical data analysis, and mathematical modeling. With a solid background in Web development he works with Python, JAVA, Django, HTML, Struts, Hibernate, Vaadin, Web Scrapping, Angular, and React. His data science skills include Python, Matplotlib, Tensorflows, Pandas, Numpy, Keras, CNN, ANN, NLP, Recommenders, Predictive analysis. He has built systems that have used both basic machine learning algorithms and complex deep neural network. He has worked in many data science projects, some of them are product recommendation, user sentiments, twitter bots, information retrieval, predictive analysis, data mining, image segmentation, SVMs, RandomForest etc.

More about this author