Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it okay to pass large variable into React state?

I have an array containing 'profiles' shown below. This is stored in a variable that is exported and I then want to render these in my component.

const people = [
  {
    name : "Chuck",
    image : "./chuck.png",
    skill : "Bowling"
  },
  {
    name : "Arno",
    image : './arno.png',
    skill : "backflips"
  }
]
export default people;

I import the variable (bare in mind it contains hundreds of data points) and in my components constructor I use the following,

constructor(props) {
    super(props)
    this.state = { data : people }

To render these I then use map to map these into a card component

render() {
    let cards = this.state.data.map(function(elem, index) {
      return (
        <Card
          name = {elem.name}
          image = {elem.image}
          skills = {elem.skill}
          key = {elem.name}
          zIndex = {1000-`${index}`}
          />
      )
    })
return (
        <Card {pass in props (name, image, skills) to child Card component} />

My question is, is this bad practice? Everything works fine. The app is similar to a dating app UI where the first card, or element in the array is popped off and we are presented with the next card. How else would you pass in a large array to use and render elements in a component?

I hope its clear what i'm doing here. Thanks

like image 592
Nooginy Avatar asked Oct 09 '19 08:10

Nooginy


People also ask

How big is too big for a React component?

50 lines is a good rule of thumb for the body of your component (for class components, that is the render method). If looking at the total lines of the file is easier, most component files should not exceed 250 lines. Under 100 is ideal. Keep your components small.

How does React handle large data?

Go to your App component and replace the code with the code below: import React from 'react'; import faker from 'faker' import { FixedSizeList as List } from "react-window"; import './App. css'; function App() { const data = new Array(1000). fill().

Why we should never update React State directly?

One should never update the state directly because of the following reasons: If you update it directly, calling the setState() afterward may just replace the update you made. When you directly update the state, it does not change this.


Video Answer


3 Answers

Having large data in React state is perfectly fine. There isn't really a limit to what you can store there and React state itself doesn't put any type of limitation on the size it can store.

The issue with large data sets appears with rendering such data. If you have for example a table that needs to render thousands of rows, it will slow down the browser. We're talking about thousands of entries though, hundreds shouldn't be a problem at all.

If you find yourself having to render thousands of data points, you can use components/libraries that help with this such as React Virtualized, which will only render the needed elements, rather than all of them.

As you can see, the problem is not what you store but what you render. My recommendation is to not worry about what you're rendering until you notice a performance issue, once you hit that point you will need to start optimizing.

Unless you're sure you will have a performance issue, optimizing from the start can make you lose time better spent elsewhere.

like image 154
Obed Parlapiano Avatar answered Oct 19 '22 12:10

Obed Parlapiano


The size of the contents of the object doesn't matter in terms of storing it in state. The object isn't copied, it's referenced.

But if people isn't managed by the component, then it isn't part of the state of the component, so state isn't where it should be. The component should just close over an import of that data, or use a context representing that data, or receive it in props, etc.

like image 31
T.J. Crowder Avatar answered Oct 19 '22 14:10

T.J. Crowder


To answer your question specifically, storing an array of objects in state is the React way.

React's documentation does not have an array of objects but Lists are made out of arrays stored in the state as in here https://reactjs.org/docs/lists-and-keys.html

It is good to note though, that it all depends on the use case and how your app will be using such state :

  • If the data never changes, it's preferable to have it in a separate variable outside of state.
  • If it changes and the data is not hindering performance, it is ok to have it in state.
  • If you start seeing that it would be better to have some optimization, you could look into a number of paradigms like pagination, caching, removing none-shown items from a list and so on.
like image 20
Abderrahmane TAHRI JOUTI Avatar answered Oct 19 '22 12:10

Abderrahmane TAHRI JOUTI