-
Notifications
You must be signed in to change notification settings - Fork 2
/
react-bouncer.js
41 lines (31 loc) · 1.44 KB
/
react-bouncer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @yaireo/react-bouncer - Wrap components with a debounce, throttle, or any other delayed-rendering method, to stop them from re-rendering often when their props change
*
* @version v1.0.3
* @homepage https://github.com/yairEO/react-bouncer
* @author Yair Even-Or <[email protected]>
*/
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _lodash = _interopRequireDefault(require("lodash.debounce"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const bouncer = function (Comp) {
let duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 300;
let method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _lodash.default;
return props => {
// https://reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate
const [dep, forceUpdate] = (0, _react.useReducer)(x => ++x, 0);
const updater = (0, _react.useCallback)(method(forceUpdate, duration), []); // call the delayed function when props change (or was re-rendered without any props)
(0, _react.useEffect)(updater, [props]);
return (0, _react.useMemo)(() => /*#__PURE__*/(0, _jsxRuntime.jsx)(Comp, { ...props
}), [dep]);
};
};
var _default = bouncer;
exports.default = _default;
//# sourceMappingURL=react-bouncer.js.map