如何在不删除reactj的前一个cookie的情况下设置和存储和更新cookie
如何编写一个cookie,该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>
);
}
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>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设
name
,<代码>电子邮件和Mobiles
都存储为数组?localstorage
仅支持字符串。但是您可以使用json.stringify()
将数组转换为JSON字符串I'm assuming
Names
,Emails
, andMobiles
are all stored as Arrays?localStorage
only supports Strings. But you can convert arrays to JSON Strings withJSON.stringify()
您可以使用数组存储日期并显示LocalStorage或Cookie的数据
you can use array to store date and display data from localStorage or cookie