【问题标题】:How to clear all stores in ReactJs on logout/signout?如何在注销/注销时清除 ReactJs 中的所有商店?
【发布时间】:2017-03-01 02:02:27
【问题描述】:

我的应用中有多个商店,它们都有构造函数。在应用程序流程中,我们的商店和本地存储会更新为不同的值。在注销时,我能够成功清除本地存储数据。

一次性清除本地存储中所有项目的代码

_clearStorage: function(){
    let len = localStorage.length;
    for (let i = len - 1; i >= 0; i--) {
        let key = localStorage.key(i);
        if (key != null && key != undefined && key.indexOf('org.') == 0) {
            localStorage.removeItem(key);
        }
    }
}

我们是否可以在 react + Flux 应用程序中刷新商店中的所有数据?有兴趣在用户从系统注销时将所有商店恢复到初始状态。

例如其中一家商店

import {EventEmitter} from "events";
import dispatcher from "../dispatchers/dispatcher";

class ItemStore extends EventEmitter{
    constructor(){
        super()
        this.itemTypes = [];                
    }

    _getter(){
        return this.itemTypes;
    }
    _setter(){
        // Some Logic
    }
}
const Stores = new ItemStore;
dispatcher.register(Stores._handleActions.bind(Stores));
window.dispatcher = dispatcher;
export default Stores;  

【问题讨论】:

  • 清除localStorage使用localStorage.clear();
  • 清除 localStorage 只是一个例子。问题是清除或重新初始化存储?如果有可能以某种方式将所有商店更新为初始状态。
  • 我知道。这就是我在 cmets 中写它的原因 :)

标签: javascript reactjs reactjs-flux


【解决方案1】:

如果你想一次性清除所有位置-

window.localStorage.clear();

它会正常工作的。

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 2021-08-02
    • 2020-09-05
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多