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

136
Views
How can I append user inputs in a single array on localStorage key in reactjs?

Every time a new input is entered, I am trying to make it so that input is appended into an array in localstorage. I am currently using localStorage.setItem('Inputs', JSON.stringify(document.getElementById("inputVal").value)) which takes the value but does not add it to any array and instead replaces the previous value. How would I go abouts adding the input into an array for localstorage? I am also wondering if I could use .appendChild()?

quick update: I have also tried

var valueS = document.getElementById("inputVal").value;
    const arr = [];
    const newArry = [
      ...arr,
      valueS
    ]
    //arr.push(valueS)
    localStorage.setItem('Data', JSON.stringify(newArry))
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just like @nIta, @Patrick Evans and @David Grosh pointed out, the const arr = [] should contain the localstorage array.

The solution that works for me is as follows:

var valueS = document.getElementById("inputVal").value;
    const arr = [JSON.parse(localStorage.getItem('Data'))];
    const newArry = [
      ...arr,
      valueS
    ]
    localStorage.setItem('Data', JSON.stringify(newArry))
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!