yoursweater
yoursweater

Reputation: 2041

Where does NgRx (or Redux) actually store data?

Obviously it's in client side memory, but what's keeping it there under the hood? LocalStorage? WebStorage?

Upvotes: 0

Views: 555

Answers (1)

markerikson
markerikson

Reputation: 67469

It's just some Javascript variables.

Here's a tiny version of a Redux store:

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 }
}

So, state is literally just a variable that points to whatever your reducer function returned.

Upvotes: 5

Related Questions