如何将数据存储在reactjs中的localstorage中
我正在尝试设置管理和匿名的两种用户,我正在尝试将其存储在LocalStorage中。因此,在这里,我如何定义帐户:
const [defaultAccount, setDefaultAccount] = useState(null, () => {
const localData = localStorage.getItem('userType');
return localData ? JSON.parse(localData) : [];
});
这就是我在管理和匿名之间区分的方式:
useEffect(() => {
if (defaultAccount === '') {
localStorage.setItem('userType', 'admin')
setConnButtonText('Wallet Connected');
} else if (defaultAccount === '') {
localStorage.setItem('userType', 'nonexist')
} else {
localStorage.setItem('userType', 'anonymous')
setConnButtonText('Wallet Connected');
}
}, [defaultAccount])
但是在我刷新页面后,即使我是管理员,它也会再次匿名。我该如何解决?因此,总而言之,我单击按钮并获取DefaultAccount,然后将USERTYPE存储为管理员。但是后来我刷新了页面,它再次匿名。我要实现的目标,在单击按钮之前,使用USERTYPE应该为空,之后应为管理或匿名。刷新页面后,由于将其存储在LocalStorage中,因此不应更改IT USERTYPE。
I am trying to set up 2 types of users which are admin and anonymous and I am trying to store them in localStorage. So here, how I defined the account:
const [defaultAccount, setDefaultAccount] = useState(null, () => {
const localData = localStorage.getItem('userType');
return localData ? JSON.parse(localData) : [];
});
And here is how I differentiate between admin and anonymous:
useEffect(() => {
if (defaultAccount === '') {
localStorage.setItem('userType', 'admin')
setConnButtonText('Wallet Connected');
} else if (defaultAccount === '') {
localStorage.setItem('userType', 'nonexist')
} else {
localStorage.setItem('userType', 'anonymous')
setConnButtonText('Wallet Connected');
}
}, [defaultAccount])
But after I refreshed the page, even though I am an admin, it comes anonymous again. How can I fix this? So, to sum up, I click the button and get defaultAccount and I store userType as an admin. But then I refresh the page and it comes anonymous again. What I am trying to achieve, before the button is clicked, userType should be empty and after, it should be admin or anonymous. After refreshing the page, since it is stored in localStorage, it userType shouldnt be changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要确定
USERTYPE
将保留什么。有一些问题:usestate
具有初始值,或者将评估初始值的函数。你们两个都通过。localstorage.getItem('usertype')
似乎它返回一个JSON对象(您做JSON.PARSE),而您却是setTiTem('usertype','一些字符串')
useseffect
在依赖关系的 array 中,您只是在字符串defaultAccount
中传递,使用
的内容是:defaultAccount
来自usertype
,但随后在本地存储中设置usertype
只有 to“ admin”
或“匿名”
也许您想做的是:
You need to decide what
userType
will hold. There's a few issues:useState
either takes in an initial value, or a function that will evaluate the initial value. You are passing them both.localStorage.getItem('userType')
seems like it returns a JSON object (you do JSON.parse), whereas you aresetItem('userType', 'some string')
useEffect
takes in an array of dependencies, you are passing in just the stringdefaultAccount
useEffect
does:defaultAccount
comes fromuserType
, but then it setsuserType
in the local storage only to"admin"
or"anonymous"
Maybe something you're looking to do is something like:
您的代码始终设置DefaultAccount的初始值为null
Your code always set initial value for defaultAccount is null