首页 > 解决方案 > 如何通过 localStorage.getItem() 恢复值

问题描述

这可能是一个新手问题,但我确实设法将值保存在 localStorage 中,我想在重新加载浏览器后恢复。我用布尔值或数字值做到了,但我不知道如何保存/加载数组。我在此过程中使用的代码:

const storedPlayerChips = localStorage.getItem('playerChips');

if (localStorage.getItem('playerChips')) {
            this.setState({
                playerChips: +localStorage.getItem('playerChips'),
            })
        } else {
            this.setState({
                playerChips: 1000,
            })
localStorage.setItem('playerChips', JSON.stringify(nextState.playerChips));

在此处输入图像描述

标签: javascriptreactjslocal-storage

解决方案


推荐阅读