项目作者: mthahzan

项目描述 :
Debounce any component actions
高级语言: JavaScript
项目地址: git://github.com/mthahzan/react-component-action-debouncer.git
创建时间: 2018-11-01T03:10:01Z
项目社区:https://github.com/mthahzan/react-component-action-debouncer

开源协议:MIT License

下载


Debouncer

This is a higher order component that can enhance any React component with a debounce effect on it’s action handlers.

NOTE - This is intended to debounce component action handlers. This is NOT suitable to something like a debounced search on input change. See usages section for example usages.

Inspiration

I got tired of testers rapidly pressing buttons and claiming they “broke” the app. So I created this debouncer to clean out erratic presses without glitching the UI.

Installation

yarn add react-component-action-debouncer or npm i react-component-action-debouncer --save

Usage

Simple usage

  1. import Debouncer from 'react-component-action-debouncer';
  2. import {SingleActionCustomComponent} from '<component/path>';
  3. const DebouncedSingleActionCustomComponent = Debouncer(SingleActionCustomComponent, 'onAction');
  4. // When rendering use the enhanced component
  5. <DebouncedSingleActionCustomComponent onAction={this.handleAction} ></DebouncedSingleActionCustomComponent>

Advanced usage

If you want more control over the debounce effect (debounce multiple actions of the same component or change the duration), you can do the following.

  1. import Debouncer from 'react-component-action-debouncer';
  2. import {SingleActionCustomComponent, MultiActionCustomComponent} from '<component/path>';
  3. const singleActionConfig = {
  4. duration: 800,
  5. type: Debouncer.TYPE.TRAILING_EDGE,
  6. propTypesToDebounce: 'onAction',
  7. };
  8. const DebouncedSingleActionCustomComponent = Debouncer(SingleActionCustomComponent, singleActionConfig);
  9. const multiActionConfig = {
  10. duration: 500,
  11. type: Debouncer.TYPE.LEADING_EDGE,
  12. propTypesToDebounce: ['onActionOne', 'onActionTwo', 'onActionThree'],
  13. };
  14. const DebouncedMultiActionCustomComponent = Debouncer(MultiActionCustomComponent, multiActionConfig);
  15. // When rendering use the enhanced components
  16. <DebouncedSingleActionCustomComponent onAction={this.handleAction} ></DebouncedSingleActionCustomComponent>
  17. <DebouncedMultiActionCustomComponent
  18. onActionOne={this.handleActionOne}
  19. onActionTwo={this.handleActionTwo}
  20. onActionThree={this.handleActionThree}
  21. ></DebouncedMultiActionCustomComponent>

Config

If you want a simple debounce effect on one action handler of a component, config can be the action name. See simple usage.
Or config can be an object with the following properties which gives you better control of the debounce effect.

  1. {
  2. // ** REQUIRED **
  3. // The prop names to intercept and add debounce effect
  4. // Can be a string (for single action) or an array of strins (for multiple actions)
  5. propTypesToDebounce: string | Array<string>,
  6. // Debounce duration in milliseconds (DEFAULT: 1000)
  7. duration: number,
  8. // Type of debounce (Default: Debouncer.TYPE.LEADING_EDGE)
  9. // We support two types of debounces. Leading Edge and Trailing Edge
  10. // `Leading Edge` will execute the action and block subsequent executions until the given `duration` elapses.
  11. // `Trailing Edge` will debounce the execution each time the action happens and executes it after the `duration` passes without any more callback calls
  12. // 'THROTTLE' will will execute the action after waiting for the `duration` to elapse blocking executions during this time.
  13. type: Debouncer.TYPE.LEADING_EDGE | Debouncer.TYPE.TRAILING_EDGE | Debouncer.TYPE.THROTTLE,
  14. }

TODO

  • Write tests