Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda