Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
Can I initialize a useState in a loop?

I use a react module that manages the use of spreadsheets: react-spreadsheet https://github.com/iddan/react-spreadsheet/

In my project, I need several spreadsheets, and therefore several tables that the user can add, modify, delete on the fly

At the beginning, I wanted to use a big useState variable where the data arrays would be concentrated in the form : stateData = [ [...arrays], [...arrays...] ]

But the module, which uses an OnChange function taking as value the setData, seems to bug when my stateData variable contains different arrays.

<Spreadsheet key={index} className='table' columnLabels={stringLabelCol} rowLabels={stringLabelRow} data={stateData[index]} onChange={setData}  />

As I can't manage everything in one UseState with this module, is it possible to create a series of UseState inside a loop ?

It seems to me that it is a bad practice but I am really blocked Thank you for reading my text... :/

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Seeing your code and your comment, I'm assuming that:

  • You have one useState named data with ALL your data in it
  • You have multiple spreadsheets
  • For each spreadsheet, you want to pass the sheetData and the onChange that updates it

The issue in your code is that you're setting onChange={setData}, meaning any change will override the WHOLE data object, not just the targeted index.

What you want to do is have your onChange function be a function that updates only the right part of your state. So you'd do it like so:

const [data, setData] = useState([])

const updateSheetData = (index, sheetData) => {
  // We clone the current state
  const tempData = [...data]
  // We update the right index
  tempData[index] = sheetData
  // This is now the new state
  setData(tempData)
}

//...
// the onChange function for our component will use our custom function
<Spreadsheet 
  key={index} 
  className='table' 
  columnLabels={stringLabelCol} 
  rowLabels={stringLabelRow} 
  data={stateData[index]} 
  onChange={(newData) => updateSheetData(index, newData)}  
/>

Then you'll also have to correctly update the data state whenever you add a new sheet or remove a sheet, to make sure the index are updated. But the idea is the same

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda