score:1
The problem is that you're setting mobileDetails
to an empty array on each form submitting. To resolve this issue, You have 2 ways:
First: Saving mobile details in an array using useState
hooks. Using this way on each page refresh you will lose your data again.
Second:(Recommended if not losing data in refresh is important to you)
Initialize your mobileDetails
array using localStorage in your handleSubmit
. Refactored code should be something like this:
const handleSubmit = () => {
//Getting data from localStorage
const savedMobileDetails = localStorage.getItem('data')
//Check if data is available to parse or seeting empty array
const mobileDetails = savedMobileDetails ? JSON.parse(savedMobileDetails) : []
//Saving new data
localStorage.setItem('data',JSON.stringify([...mobileDetails,detail]))
}
Source: stackoverflow.com
Related Query
- Localstorage is overriding the previous store data at index 0 instead of storing new data at index 1. Can anyone suggest me what should I do?
- How can I store previous values instead of storing the value in array of objects in react state
- Which is the best way to store a list of data in localStorage and display them when searched using a React app
- Duplicating authentication data between the store and localStorage
- Where should I store my JWT in 2019 and is the localStorage really not secure?
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- How to fetch data in getInitialProps with redux-saga.Then get the response from redux store while in the getInitialProps method?
- React testing - How can I mock or inject data in the store to be provided as props to component
- Redux saga - the point to skip API requsts if data already exists in redux store
- Differentiate between the previous element and current element from the data of a Flatlist in react native
- Every change of the input is stored separately as data (ex : "t", "te", "tes", "test" instead of just having "test")
- Should I store never changing (bootstrapped) data in the store?
- Rendered more hooks than during the previous render. when posting form data with React Hooks
- Should the action or store be responsible for transforming data when using React + Flux?
- How to change the data stored in LocalStorage when clicking on element in react?
- React-Table Filtering unexpected behavior : The table header is updated (and the filter itself) instead of updating just the data
- Gatsby: Using localStorage to store data
- performance: connecting to the Redux store vs passing data through props
- Ant design table row merge based on data instead of index value
- Why it gets the correct data from localStorage only after refreshing the page in React
- saving authentication data on the LocalStorage react js
- After updating the data of store in redux, this.props and nextProps are same
- Append data to the nested redux store
- React JS: previousState in setState is not keeping the data of the previous state after the state is updated
- React, Redux: how to avoid loading data into the store twice
- What is the best way to store previous response from an API and compare with new response in Nodejs?
- How to get the data from Redux store using class components in React
- how to store date in the firebase with type 'timestamp' instead of 'map' from reactjs
- There is a time delay in my todolist, its not uploading data quickly to the localstorage
- React input field mirrors the data from previous input
More Query from same tag
- How to test react component with axios request in useEffect?
- How to pass array object from select tag and pass values into API?
- simple questions of callbacks with and without an array function
- Encapsulate a NavLink component and style it with Styled Component
- How to wait for javascript promise to return value before continuing execution?
- Is it possible to include minute and seconds data in a time series apex chart?
- React CORS being blocked when uploading to s3
- Apex chart doesn't display immediately
- UnhandledPromiseRejectionWarning:
- Can't figure out why draft-js is not shown
- How do I access the dataProvider directly in react-admin?
- One component overlapping on other in react.js app
- Adding proptypes to unnamed anonymous default exported functions - e.i "export default () =>{}"
- Check if shortcut is present on homescreen as in normal mobile apps
- State Variables become null When UseEffect hook is used
- Uploading an image with multer / react-dropzone
- React Modal component is not working in Nextjs
- push data to array in Firebase
- Uncaught Invariant Violation: Maximum update depth exceeded
- React should form elements be a separate component?
- JSX element type 'Line' is not a constructor function for JSX elements
- Add and remove data to table - React
- How to have a loading screen until all my components are mounted in React
- What is the proper way to deal with node_modules in git?
- How to round the border corners inside the style prop of the VictoryLegend component?
- How to link to section in react toggle menu
- How to change the state in react with a form
- Firebase authentication returning false when reloading the page
- getDerivedStateFromProps returned undefined
- Using redux and got Parsing error: Unexpected token