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

105
Views
How to update particular element of local storage in reactjs

I am currently storing accessToken and user's info in local storage which is 'userInfo'

Where token will be stored in userInfo.token and userIno will be stored in userInfo.info

I want to update the token inside the userInfo and also want to keep userInfo.info unchanged, Is there any way to do this?

currently I am using this code:

localstorage.setItem('userInfo', data.token)

but it's deleting userInfo.info

userInfo structure:

{"token": eykjdbvksbkvsbks89all, "userinfo": {"email": veera@gmai.com} }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need to update the entire user object in localStorage.

Example:

const user = {"token": "adkadjhk2h3hkhkhkh", "userinfo": {"email": ":veera@gmai.com" } };

localStorage.setItem('userInfo', JSON.stringify(user))

const userInfo = JSON.parse(localStorage.getItem('userInfo'));

const newUpdatedUserInfo = {
  ...userInfo,
  "token": "new-token-adkadjhk2h3hkhkhkh"
};

localStorage.setItem('userInfo', JSON.stringify(newUpdatedUserInfo))
about 4 years ago · Juan Pablo Isaza Report

0

Try this Code example:

 localStorage.setItem('userInfo',JSON.stringify({name:'reza',age:20}));
    const data = window.localStorage.getItem('userInfo')
    const newData = JSON.parse(data)
    localStorage.setItem('userInfo',JSON.stringify({...newData,age:44}))
about 4 years ago · Juan Pablo Isaza Report

0

const userInfo = window.localStorage.getItem('userInfo')
const parsedUserInfo = JSON.parse(userInfo)
localStorage.setItem('userInfo', JSON.stringify({...parsedUserInfo,token: data.token }))

You can try this snippet.

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!