npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-throttle

v0.3.0

Published

Throttles/debounces handlers of a child element

Downloads

5,138

Readme

react-throttle

Build Status codecov.io Dependency Status devDependency Status bitHound Overall Score Known Vulnerabilities

react-throttle is an npm package which contains two React components: Throttle and Debounce. These components can be wrapped around a child component, the event handlers of which will be either throttled or debounced depending on which component you choose.

Problem

Oftentimes, events are fired more often than we really care about. For example, when scrolling down a page, the onScroll event will fire many times per second. If our event handler is even slightly complex, this will result in jank.

Pages which employ incremental searches are quite common on the Web. Typically an <input> element is used with an onChange handler, which sends an XHR to the server to request results. If the user is typing quickly, we don't want to bombard the server with a request for each keystroke, as this is a waste of resources.

Solution

By throttling or debouncing our event handlers, we can dramatically reduce the amount of times the handler is called. By using these methods, we are able to choose the frequency with which we respond to events.

There are two methods by which we can lower the frequency of our event handler calls:

  • Throttling

    This method revolves around a time interval. For example, by specifying a time interval of 100ms, this will ensure that the event handler is not called more than once per 100 milliseconds. This is handy for use cases such as the scrolling example above, where the handler may be called dozens of times in a 100 millisecond timeframe. Using the throttling method, we drastically cut down how often the handler is called.

  • Debouncing

    Sometimes we only want to respond to an input when it is, or seems to be, complete. Again, we specify a time interval, but this time it has a different meaning. If we specify 100ms in this case, it means that our event handler will only be called if at least 100ms have passed since the last event. In the case of the incremental search box, this means that the typed input will only be processed 100ms after the user's last keystroke.

Usage

The components are both used in a similar manner:

Throttle

import { Throttle } from 'react-throttle';

<Throttle time="200" handler="onChange">
    <input onChange={() => console.log('input')} />
</Throttle>

Debounce

import { Debounce } from 'react-throttle';

<Debounce time="400" handler="onChange">
    <input onChange={() => console.log('input')} />
</Debounce>

Both these components take a prop called handler which specifies the handler on the child component to throttle or debounce. An array of handler names can also be passed through handlers if the need arises.

License

MIT

Bug Reports

Please file bug reports using GitHub issues. Thank you!

Pull Requests

If you would like to contribute to this project, feel free. Clean, well-tested, well-documented code is appreciated. Thanks!