A React higher order component to debounce the rendering of your React components
react-debounce-render
is a Higher Order Component that wraps your react components and debounces their rendering.
This method can be used to prevent extra renders when a react component rapidly receives new props by delaying the triggering of the render until updates become less frequent. Doing so will improve the overall rendering time of the application, thus improve the user experience.
It uses lodash debounce under the hood, which means that it can be configured just like lodash debounce.
Sometimes it's difficult, impractical or impossible to batch props updates before they're passed down to a react component, resulting in a react component being rendered too often while only the last props update might have been useful and should have triggered a rendering of the component. In these cases, debouncing the rendering of a react component may be the only solution to improve performance.
react-debounce-render
is a react component that will wrap any of your react components, persist the last props update that was received while discarding the previous ones,
and only rendering the wrapped component when no new updates has been received in the last few milliseconds by default. Since the debounce function used under the hood
is lodash's debounce, you may also pass in a few options such as the number of milliseconds to delay the render by, a maximum delay, or whether to call render on a leading or trailing edge of the timeout.
npm install react-debounce-render
import debounceRender from "react-debounce-render";
const debouncedMyReactComponent = debounceRender(MyReactComponent);
// do whatever you want with the debounced component such as connecting to a redux store:
connect((state) => {
myProp: (state) => getProp(state);
})(debouncedMyReactComponent);
See lodash debounce for all options. Debounce render takes its parameters similarily to lodash debounce:
To trigger the debounce on the leader edge:
import debounceRender from "react-debounce-render";
const debouncedMyReactComponent = debounceRender(MyReactComponent, 100, {
leading: true,
});
Or to ensure that at least one refresh happens every second:
import debounceRender from "react-debounce-render";
const debouncedMyReactComponent = debounceRender(MyReactComponent, 100, {
maxWait: 1000,
});
Use debounce
instead of debounceRender
to compose with other HOCs and some compose utility function.
maximizing composability convention
import { debounce } from "react-debounce-render";
import { withStyles } from "@material-ui/core/styles";
import { compose } from "ramda";
// ...
export default compose(
withStyles(styles),
debounce(200, { maxWait: 400 })
)(MyReactComponent);
debounce
higher order function as 2nd export for use in redux/compose patterns. See maximizing composability convention for more info. Thanks CurriedFloat for the contribution!There is no breaking change in this version despite the major version change, this is only to highlight the fact that most of the internal implementation has changed since PR #12.
lodash
dependency since we're using lodash/debounce
. Thanks @yched for raising the issue!lodash/debounce
directly. Thanks @faizrr for raising the issue!peerDependencies
in package.json and accept major versions greater than 15. Resolves Issue #4. Thanks @TheSharpieOne for raising the issue.import debounceRender from 'react-debounce-render'
should now consistently work. See Issue #2. Thanks @CameronAckermanSEL for raising the issue.
As a result, using a destructuring assignment in the import statement (import { debounceRender } from 'react-debounce-render''
) shouldn't accidentally work anymore which is a potential breaking change.