1.0.3 • Published 5 years ago

@teneff/react-debounce v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

React <Debounce /> Component

Using render props to debounce a callback. Useful for preventing multiple callback executions on scroll, spam-clicking, typing etc.

NPM version Build Status Coverage Status GitHub issues GitHub stars Slack

Install

  • using npm
npm install @teneff/react-debounce
  • using yarn
yarn add @teneff/react-debounce

Live Demo

codesandbox-img

Usage

import Debounce from '@teneff/react-debounce';

class MyComponent extends React.Component {
  handleClick = () => {
    console.info("event handler will be executed once after multiple clicks");
  };

  handleKeyUp = value => {
    console.info("event handler will be executed when you stop typing", value);
  };

  render() {
    return (
      <div>
        <Debounce callback={this.handleClick} delay={300}>
          {onClick => <button onClick={onClick}>click me, wait, profit</button>}
        </Debounce>
        <Debounce callback={this.handleKeyUp} delay={500}>
          {onKeyUp => (
            <input
              type="text"
              onKeyUp={e => onKeyUp(e.target.value)}
              placeholder="Start typing..."
            />
          )}
        </Debounce>
      </div>
    );
  }
}