首页 > 解决方案 > NgRx(或 Redux)实际上在哪里存储数据?

问题描述

显然它在客户端内存中,但是是什么将它保存在引擎盖下?本地存储?网络存储?

标签: reduxngrx

解决方案


这只是一些 Javascript 变量。

这是 Redux 商店的一个小版本:

function createStore(reducer) {
    var state;
    var listeners = []

    function getState() {
        return state
    }

    function subscribe(listener) {
        listeners.push(listener)
        return function unsubscribe() {
            var index = listeners.indexOf(listener)
            listeners.splice(index, 1)
        }
    }

    function dispatch(action) {
        state = reducer(state, action)
        listeners.forEach(listener => listener())
    }

    dispatch({})

    return { dispatch, subscribe, getState }
}

所以,state实际上只是一个变量,它指向你的 reducer 函数返回的任何内容。


推荐阅读