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

134
Views
Best way to set multiple variables in local storage

I am working on a form for adding a recipe to my website and I want to store the contents of each form input inside of local storage so that they won't be lost on page refresh. I have around 10 inputs on my form with their contents all held in react states. Would it be better to keep each variable in an object and store that as one local storage item to be changed on every variable state change, or keep each variable in a separate local storage item.

Option 1:

const [a, setA] = useState('')
const [b, setB] = useState('')
// Many more variables...

useEffect(() => {
  const recipes = JSON.parse(localStorage.getItem('recipes'))
  if (recipes) {
    setA(recipes.a)
    setB(recipes.b)
    // For each variable...
  }
}, [])

// Update 'recipe' item in local storage on any form input state change
useEffect(() => {
  localStorage.setItem('recipe', JSON.stringify({a, b}))
}, [a, b])

Option 2:

// Set each state to it's local storage item or an empty string if local storage is empty
const localA = localStorage.getItem('a')
const [a, setA] = useState(JSON.parse(() => (localA ? localA : '')))
const localB = localStorage.getItem('b')
const [b, setB] = useState(JSON.parse(() => (localB ? localB : '')))

// Set each individual local storage item on state change
useEffect(() => {
  localStorage.setItem('a', JSON.stringify(a))
}, [a])

useEffect(() => {
  localStorage.setItem('b', JSON.stringify(b))
}, [b])

I just want to know what will be the best for a larger amount of variables or if there is even still a better way to go about solving this in the first place. Thanks a lot!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you keep the form state in object form you'll have to serialize it to put it into local storage, so there's some cost with serializing and deserializing that object. The alternative of storing each field's value independently wouldn't have this deserialization overhead. I don't think the overhead of serialization is a concern for a simple form, so I'd take the object approach.

Perhaps an intermediate would be to use the form object approach, but only persist it when the window is being closed or unloaded.

window.onclose = () => {
    // Save the form state to local storage.
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use multiple variables,

If you want to minimize the serialization just use this helper file to reuse the functionalities and to have your code clean.

Helper file: https://gist.github.com/scrubmx/a240a912a475ab0a2c43

If you are going to build a larger app with redux state management, I will strongly recommend you to use redux-persist. You no need to worry about serializing and deserializing. There are a lot of features you can use.

Documentation: https://www.npmjs.com/package/redux-persist

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!