JavaScript SDK store

Snipcart's JavaScript SDK uses Redux as a state management tool. Therefore, some Redux store methods are available.

getState

The getState method returns the current state tree of the application. In this case, the return value is of type SnipcartState.

Snipcart.store.getState();

subscribe

The subscribe method triggers a callback every time an action is dispatched. It is often used to check if a change occurred in the state of the application. When invoked, the return function of the subscribe method will remove the change listener.

let currentValue;
const unsubscribe = store.subscribe(() => {
    let previousValue = currentValue;
    currentValue = Snipcart.store.getState();

    if (previousValue !== currentValue) {
        // Anything goes...
    }
});

// unsubscribe();