redux-saga-sugar

redux-saga utilities.

Usage no npm install needed!

<script type="module">
  import reduxSagaSugar from 'https://cdn.skypack.dev/redux-saga-sugar';
</script>

README

redux-saga-sugar

redux-saga utilities.

npm version build status npm downloads

Getting started

Install

$ npm i -S redux-saga-sugar

Documentation

pollingSagaMiddleware

实时中间件

export function* rootSaga() {
  yield fork(Sugar.pollingSagaMiddleware);
}

const actions = Sugar.createActions({
  beginPolling: {
    pollingSUrl: Urlmap.polling,
    types: [load, pollingSuccess, failure],
  },
  stopPolling: {
    type: pollingSuccess,
    stopPolling: true,
  },
});

createActions / createAction

  • createAction(SWITCH_TAB)

function(payload) {
  return {
    type: SWITCH_TAB,
    payload
  };
}
  • createAction({ type: SWITCH_TAB })

function() {
  return {
    type: SWITCH_TAB,
  };
}
import Sugar from 'redux-saga-sugar';

export const actions = Sugar.createActions({
  loadBasicData: {
    url: getUrl('loadBasicData'),
    types: [LOAD_BASIC_DATA_LOAD, LOAD_BASIC_DATA_SUCCESS, LOAD_BASIC_DATA_FAILURE],
  },
  loadData: {
    url: getUrl('loadData'),
    types: [LOAD_DATA_LOAD, LOAD_DATA_SUCCESS, LOAD_DATA_FAILURE],
  },
  switchTab: SWITCH_TAB,
  beginLoad: BEGIN_LOAD,
});

createWatch

yield Sugar.createWatch(pattern, saga, ...args)

yield fork(function* () {
  yield takeEvery(pattern, saga, ...args);
});

例子:

export function saga() {
  yield Sugar.createWatch([SWITCH_TAB, BEGIN_LOAD], loadDataSaga);
}

get

yield Sugar.get(path);

yield select(state => {
  return _.get(state, path);
});

例子:

function* loadDataSaga() {
  const id = yield Sugar.get(`${currPath}.id`);
  yield put(actions.loadData({ id }));
}

License

MIT