无法执行“setItem”在“Storage”上:设置“state”的值;超出配额(localStorage.clear() 不起作用)

发布于 2025-01-16 20:39:28 字数 1275 浏览 0 评论 0原文

在我的 React 项目中,我使用 Redux 保存序列化状态:

const saveState = (state: any) => {
    try {
        const serializedState = JSON.stringify(state);

        localStorage.removeItem('state');
        localStorage.clear();
        localStorage.setItem('state', serializedState);

    } catch (e) {
        console.log(e);
        let _lsTotal = 0, _xLen, _x; for (_x in localStorage) { if (!localStorage.hasOwnProperty(_x)) { continue; } _xLen = ((localStorage[_x].length + _x.length) * 2); _lsTotal += _xLen; console.log(_x.substr(0, 50) + " = " + (_xLen / 1024).toFixed(2) + " KB") }; console.log("Total = " + (_lsTotal / 1024).toFixed(2) + " KB");
    }};

由于我遇到“设置‘状态’的值超出配额”错误,我尝试清除本地存储(见上文)。但我仍然收到此错误,并且 catch 部分中的代码显示 0.00KB 的存储:

DOMException: Failed to execute 'setItem' on 'Storage': Setting the value of 'state' exceeded the quota.
at saveState (http://localhost:xxxx/static/js/main.chunk.js:16322:18)
at http://localhost:xxxx/static/js/main.chunk.js:16350:5
at invokeFunc (http://localhost:xxxx/static/js/1.chunk.js:276358:19)
at trailingEdge (http://localhost:xxxx/static/js/1.chunk.js:276403:14)
at timerExpired (http://localhost:xxxx/static/js/1.chunk.js:276391:14)
Store.ts:1967 Total = 0.00 KB

如何避免此错误?

In my React project I use Redux where I save the serialized state:

const saveState = (state: any) => {
    try {
        const serializedState = JSON.stringify(state);

        localStorage.removeItem('state');
        localStorage.clear();
        localStorage.setItem('state', serializedState);

    } catch (e) {
        console.log(e);
        let _lsTotal = 0, _xLen, _x; for (_x in localStorage) { if (!localStorage.hasOwnProperty(_x)) { continue; } _xLen = ((localStorage[_x].length + _x.length) * 2); _lsTotal += _xLen; console.log(_x.substr(0, 50) + " = " + (_xLen / 1024).toFixed(2) + " KB") }; console.log("Total = " + (_lsTotal / 1024).toFixed(2) + " KB");
    }};

Since I came upon the 'Setting the value of 'state' exceeded the quota' error I tried to clear the local storage (see above). Yet I still get this error, and the code in the catch part shows 0.00KB of the storage:

DOMException: Failed to execute 'setItem' on 'Storage': Setting the value of 'state' exceeded the quota.
at saveState (http://localhost:xxxx/static/js/main.chunk.js:16322:18)
at http://localhost:xxxx/static/js/main.chunk.js:16350:5
at invokeFunc (http://localhost:xxxx/static/js/1.chunk.js:276358:19)
at trailingEdge (http://localhost:xxxx/static/js/1.chunk.js:276403:14)
at timerExpired (http://localhost:xxxx/static/js/1.chunk.js:276391:14)
Store.ts:1967 Total = 0.00 KB

How to avoid this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2025-01-23 20:39:28

如何避免这个错误?

不要将巨大对象放入状态。根据评论,您的对象看起来有 6006565 长。这是很多位!

How to avoid this error?

Don't put huge objects into state. Per the comments, it looks like your object was 6006565 long. That's a lot of bits!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文