1.2.3 • Published 4 years ago

use-rematch-reducer v1.2.3

Weekly downloads
37
License
MIT
Repository
github
Last release
4 years ago

use-rematch-reducer

Features

  • typescript typo supported by rematch
  • async dispatch

Install

npm install use-rematch-reducer --save

Usage

screenshots

// import
import { useRematchReducer } from 'use-rematch-reducer';

// create hook in compnent
const [state, dispatch] = useRematchReducer({
  name: 'use-rematch-reducer',
  state: {
    cnt: 0,
    loading: false,
  },
  reducers: {
    add: (state: State, payload?: number) => {
      return {
        ...state,
        cnt: payload ? state.cnt + payload : state.cnt + 1,
      };
    },
    toggleLoading: (state: State) => {
      return {
        ...state,
        loading: !state.loading,
      };
    },
  },
  effects: {
    async asyncAdd(payload: number, state: State) {
      this.toggleLoading();
      setTimeout(async () => {
        this.add(payload);
        this.toggleLoading();
      }, 1000);
    },
  },
});

// use in component
<div>
  <a style={{ marginRight: '16px' }} onClick={() => dispatch.add()}>
    add
  </a>
  <a style={{ marginRight: '16px' }} onClick={() => dispatch.asyncAdd(1)}>
    async add after 1s
  </a>
  <a style={{ marginRight: '16px' }} onClick={() => dispatch.add(-1)}>
    reduce
  </a>
</div>

see full code in examples

TODO

    • add test part

Author

👤 JW

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator

1.2.3

4 years ago

1.1.0

4 years ago

1.0.6

4 years ago

0.1.3

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago