Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!