如何在不删除 ReactJs 中的前一个 cookie 的情况下设置、存储和更新 cookie

How to set and store and update a cookie without removing previous one in ReactJs

如何编写一个 cookie 来在用户提交表单时保存输入的值......以及当再次提交表单时,更新 cookie 而不删除以前保存的值

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>
    );
  }

您可以使用数组来存储日期并显示来自 localStorage 或 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>
    );
  }

我假设 NamesEmailsMobiles 都存储为数组?

localStorage 仅支持字符串。但是您可以使用 JSON.stringify()

将数组转换为 JSON 字符串
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: names ? JSON.parse(names) : null,
    Emails: emails ? JSON.parse(emails) : null,
    Mobiles: mobiles ? JSON.parse(mobiles) : null
  }
}