项目作者: manhhailua

项目描述 :
A no headache middleware helper for redux-saga.
高级语言: JavaScript
项目地址: git://github.com/manhhailua/redux-unfold-saga.git
创建时间: 2019-04-19T15:40:49Z
项目社区:https://github.com/manhhailua/redux-unfold-saga

开源协议:MIT License

下载


redux-unfold-saga

npm
npm
NPM
Build Status
codecov
semantic-release
Snyk Vulnerabilities for npm package

A no headache middleware helper for redux-saga.

Getting started

Install

  1. npm install --save redux-unfold-saga

or

  1. yarn add redux-unfold-saga

Example repo

If you want a quick demo repo in real life.

Usage example

  • index.js
  1. import React from 'react';
  2. import { createAction } from 'redux-unfold-saga';
  3. const queryPosts = createAction('QUERY_POSTS');
  4. export default class PostList extends React.Component {
  5. state = {
  6. isLoading: false,
  7. posts: [],
  8. };
  9. componentDidMount() {
  10. this.dispatchQueryPosts();
  11. }
  12. dispatchQueryPosts = () => {
  13. this.props.dispatch(
  14. queryPosts(
  15. {
  16. type: 'HOT',
  17. },
  18. {
  19. onBeginning: () => {
  20. // Do something before the query
  21. this.setState({ isLoading: true });
  22. },
  23. onFailure: error => {
  24. // Do something in case of caught error
  25. },
  26. onSuccess: posts => {
  27. // Do something after the query succeeded
  28. this.setState({ posts });
  29. },
  30. onFinished: () => {
  31. // Do something after everything is done
  32. this.setState({ isLoading: false });
  33. },
  34. },
  35. ),
  36. );
  37. };
  38. render() {
  39. return (
  40. <View>
  41. {this.state.isLoading && <Text>Loading...</Text>}
  42. {this.state.posts.map(post => (
  43. <Text>{post.title}</Text>
  44. ))}
  45. </View>
  46. );
  47. }
  48. }
  • saga.js
  1. import { unfoldSaga } from 'redux-unfold-saga';
  2. function* takeQueryPosts({ payload }) {
  3. yield unfoldSaga({
  4. handler: async () => {
  5. const posts = await queryPosts(payload);
  6. return posts;
  7. },
  8. key: 'QUERY_POSTS',
  9. });
  10. }
  11. function* defaultSaga() {
  12. yield takeLatest('QUERY_POSTS', takeQueryPosts);
  13. }

API

Table of Contents

createActionTypeOnBeginning

Create onBeginning action type

Parameters

Examples

  1. createActionTypeOnSuccess('DO_SOMETHING') // 'DO_SOMETHING_BEGAN'

Returns string

createActionTypeOnFailure

Create onFailure action type

Parameters

Examples

  1. createActionTypeOnSuccess('DO_SOMETHING') // 'DO_SOMETHING_FAILED'

Returns string

createActionTypeOnFinish

Create onFinish action type

Parameters

Examples

  1. createActionTypeOnSuccess('DO_SOMETHING') // 'DO_SOMETHING_FINISHED'

Returns string

createActionTypeOnSuccess

Create onSuccess action type

Parameters

Examples

  1. createActionTypeOnSuccess('DO_SOMETHING') // 'DO_SOMETHING_SUCCEEDED'

Returns string

createAction

Create an action for real life usage inside or even outside of a component

Parameters

Examples

Inside of a component

  1. const queryPosts = createAction('QUERY_POSTS');
  2. this.props.dispatch(
  3. queryPosts(
  4. {
  5. category: 'HOT',
  6. },
  7. {
  8. onBeginning: () => {
  9. // Do something before the query
  10. this.setState({ isLoading: true });
  11. }
  12. onFailure: (error) => {
  13. // Do something in case of caught error
  14. }
  15. onSuccess: (posts) => {
  16. // Do something after the query succeeded
  17. }
  18. onFinished: () => {
  19. // Do something after everything is done
  20. this.setState({ isLoading: false });
  21. }
  22. },
  23. ),
  24. );

Outside of a react component

  1. store.dispatch(
  2. queryPosts({
  3. type: 'HOT',
  4. }),
  5. );

Inside another saga

  1. const queryPosts = createAction('QUERY_POSTS');
  2. function* takeQueryPosts() {
  3. yield put(queryPosts());
  4. }

Returns UnfoldSagaActionType

unfoldSaga

Common saga helper that unifies handling side effects into only one standard form

Parameters

  • body UnfoldSagaHandlerType
    • body.handler Function Main handler function. Its returned value will become onSuccess callback param
    • body.key string Action type
  • callbacks UnfoldSagaCallbacksType (optional, default {})
    • callbacks.onBeginning Function This callback will be called after onBeginning action is dispatched.
    • callbacks.onFailure Function This callback will be called after onFailure action is dispatched. It will only be called in case of error.
    • callbacks.onFinish Function This callback will be called after onFinish action is dispatched.
    • callbacks.onSuccess Function This callback will be called after onSuccess action is dispatched. It will not be called in case of error.
  • options UnfoldSagaOptionsType (optional, default {})
    • options.stateless boolean This ensures if redux actions will be triggered

Examples

  1. function* takeQueryPosts({ payload: { category } }) {
  2. yield unfoldSaga({
  3. handler: async () => {
  4. const posts = await queryPosts({ category });
  5. return posts;
  6. },
  7. key: 'QUERY_POSTS',
  8. });
  9. }
  1. function* takeQueryCategories({ payload: { category } }) {
  2. yield unfoldSaga({
  3. *handler() => {
  4. const categories = yield call(queryPosts, { category });
  5. return categories;
  6. },
  7. key: 'QUERY_CATEGORIES',
  8. });
  9. }
  10. function* defaultSaga() {
  11. yield takeLatest('QUERY_POSTS', takeQueryPosts);
  12. yield takeLatest('QUERY_CATEGORIES, takeQueryCategories);
  13. }

Returns Saga

License

MIT © Manh Pham - manhpt.com