Part of a collection of Higher-Order Components for React, especially useful with Recompose.
Helps to throttle handlers like onChange
.
yarn add @hocs/throttle-handler
throttleHandler(
handlerName: string,
interval?: number,
leadingCall?: boolean
): HigherOrderComponent
import React from 'react';
import { compose, withState, withHandlers } from 'recompose';
import throttleHandler from '@hocs/throttle-handler';
const Demo = ({ count, onButtonClick }) => (
<div>
<h1>{count}</h1>
<button onClick={onButtonClick}>CLICK ME FAST</button>
</div>
);
export default compose(
withState('count', 'setCount', 0),
withHandlers({
onButtonClick: ({ count, setCount }) => () => setCount(count + 1)
}),
throttleHandler('onButtonClick', 1000)
)(Demo);