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

191
Vistas
React state hook 2D array first element is always empty

I have a 2d array, which I can push elements to. The issue i'm running into is the fact that when i print the contents, there is always a blank element at index 0. I do not know where this is coming from?

const [array, setArray] = useState([[]]);

// call this code 4 times

const updatedArray = [...array, ['test', 'test2']];
setArray(updatedArray);

enter image description here

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

0

The Problem here is

...array

since the initial value of the array is [].

So array[0] would be equal to [] and you append your new values beginning from array[1] on.

What you want to do is const updatedArray = [['test', 'test2']]; since you do not want the initial value to be kept.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Sometime this issue occurs with state.
In case of hooks, you should use useEffect hook, As below-

const [fruit, setFruit] = useState('');

useEffect(() => {
  console.log('Fruit', fruit);
}, [fruit])

This saved my day, Hope will help you too!!!

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