Home > Software engineering >  How to set and store and update a cookie without removing previous one in ReactJs
How to set and store and update a cookie without removing previous one in ReactJs

Time:04-26

How to write a cookie that saves inputs values when the user submits the form..... And when the form is submitted again, update the cookie without deleting the previously saved values

export default function setcookie() {
  const [ name, setName ] = useState('');
  const [ email, setEmail ] = useState('');
  const [ mobile, setMobile ] = useState('');


  const cookie = () => {
    localStorage.setItem("Names", name);
    localStorage.setItem("Emails", email);
    localStorage.setItem("Mobiles", mobile);
  }
  const Data = () => {

    const names = localStorage.getItem("Names");
    const emails = localStorage.getItem("Emails");
    const mobiles = localStorage.getItem("Mobiles");

    return names   emails   mobiles;
  }
    return (
        <div>
        <div className='page-wrapper d-inline-block'>
        <TextField id="standard_name" onChange={(e) => setName(e.target.value)} value={name} type="text" name="name" dir='rtl' label="FullName" variant="standard" />
        <TextField id="standard_email"onChange={(e) => setEmail(e.target.value)} value={email} type="text" name="email" dir='rtl' label="Email" variant="standard" />
        <TextField id="standard_number"onChange={(e) => setMobile(e.target.value)} value={mobile} type="number" name="mobile" dir='rtl' label="Phone Number" variant="standard" />
        <TextField id="standard-message" type="text" name="message" dir='rtl' label="Message" variant="standard" />
        <All.Button variant='outlined' name="btn" onClick={cookie}>sa</All.Button>
          </div>
          <Data />
      </div>
    );
  }

CodePudding user response:

you can use array to store date and display data from localStorage or cookie

export default function setcookie() {
  const [ name, setName ] = useState('');
  const [ email, setEmail ] = useState('');
  const [ mobile, setMobile ] = useState('');


  const cookie = () => {
    let data = {
     Names: name,
     Emails:email,
     Mobiles:mobile
   }
    let Datas = localStorage.getItem("Datas") ? localStorage.getItem("Datas") : []
    Datas.push(data)
    localStorage.setItem("Datas", Datas);
  }
  const Data = () => {

    const Datas= localStorage.getItem("Datas");
    if (Datas){
     let data =  Datas[Datas.length - 1]
     return data.name     data.emails   data.mobiles;
   }

    return '';
  }
    return (
        <div>
        <div className='page-wrapper d-inline-block'>
        <TextField id="standard_name" onChange={(e) => setName(e.target.value)} value={name} type="text" name="name" dir='rtl' label="FullName" variant="standard" />
        <TextField id="standard_email"onChange={(e) => setEmail(e.target.value)} value={email} type="text" name="email" dir='rtl' label="Email" variant="standard" />
        <TextField id="standard_number"onChange={(e) => setMobile(e.target.value)} value={mobile} type="number" name="mobile" dir='rtl' label="Phone Number" variant="standard" />
        <TextField id="standard-message" type="text" name="message" dir='rtl' label="Message" variant="standard" />
        <All.Button variant='outlined' name="btn" onClick={cookie}>sa</All.Button>
          </div>
          <Data />
      </div>
    );
  }

CodePudding user response:

I'm assuming Names, Emails, and Mobiles are all stored as Arrays?

localStorage only supports Strings. But you can convert arrays to JSON Strings with JSON.stringify()

const handleFormSubmit = () => {
  // validate form data or whatever...
  saveInputsToLocalStorage()
}

const saveInputsToLocalStorage = () => {
  const prevSavedInputs = loadInputsFromLocalStorage()
  
  for (const input in prevSavedInputs) {
    let inputArray = prevSavedInputs[input] || []
    
    let value
    switch (input) {
      case 'Names':
        value = name
        break
      case 'Emails':
        value = email
        break
      case 'Mobiles':
        value = mobile
        break
    }
    
    localStorage.setItem(input, JSON.stringify([...inputArray, value]))
  }
}

const loadInputsFromLocalStorage = () => {
  const names = localStorage.getItem('Names')
  const emails = localStorage.getItem('Emails')
  const mobiles = localStorage.getItem('Mobiles')
  
  return {
    Names: JSON.parse(names),
    Emails: JSON.parse(emails),
    Mobiles: JSON.parse(mobiles)
  }
}
  • Related