JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 59
  • Score
    100M100P100Q16622F
  • License MIT

tiny redux store

Package Exports

  • redux-micro
  • redux-micro/dist/index.cjs.js
  • redux-micro/dist/index.esm.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (redux-micro) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

redux-micro

Build Status Coverage Status

tiny redux store with effects support

1kB (minified + gzipped)

npm install -S redux-micro
yarn add redux-micro

Usage

type State = {
  alpha: number;
  beta: Record<string, string>;
  gamma?: { isHappy?: boolean };
};

enum Action {
  clear = "clear",
  add = "add",
  set = "set",
  request_happy = "request_happy"
  request_happy_success = "request_happy_success"
}

type Requests = {
  [Action.clear]: undefined;
  [Action.add]: number;
  [Action.set]: { key: string; value: string };
  [Action.request_happy]: undefined;
  [Action.request_happy_success]: boolean;
};

const reducers: ReducerMap<State, Requests> = {
  [Action.clear]: (state, action, payload) => {
    return { ...state, a: 0, b: {} };
  },
  [Action.add]: (state, action, payload) => {
    return { ...state, a: state.a + payload };
  },
  [Action.set]: (state, action, payload) => {
    return { ...state, beta: { ...state.beta, [payload.key]: payload.value} };
  },
  [Action.request_happy]: (state, action, payload, dispatch) => {
    fetch("/api/is-happy").then(response => {
      if (response.ok) {
        dispatch(Action.request_happy_success, true);
      } else {
        dispatch(Action.request_happy_success, false);
      }
    }).catch(() => {
      dispatch(Action.request_happy_success, false);
    })
    return state;
  },
  [Action.request_happy_success]: (state, action, payload) => {
    return { ...state, gamma: {isHappy: payload} };
  },
};

const initialState: State = {
  alpha: 0,
  beta: {},
};

const store = new Store<State, Requests>(initialState, reducers);

store.subscribe((state, action, payload) => console.log(action, payload));

store.dispatch(Action.request_happy, undefined);