UNPKG

2.36 kBJavaScriptView Raw
1"use strict";
2
3var _typeof = require("@babel/runtime/helpers/typeof");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = exports.RenderBlock = void 0;
8var React = _interopRequireWildcard(require("react"));
9function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
10function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
11/* istanbul ignore file */
12
13function useRenderTimes(props, debug) {
14 // Render times
15 var timesRef = React.useRef(0);
16 timesRef.current += 1;
17
18 // Props changed
19 var propsRef = React.useRef(props);
20 var keys = [];
21 Object.keys(props || {}).map(function (key) {
22 var _propsRef$current;
23 if ((props === null || props === void 0 ? void 0 : props[key]) !== ((_propsRef$current = propsRef.current) === null || _propsRef$current === void 0 ? void 0 : _propsRef$current[key])) {
24 keys.push(key);
25 }
26 });
27 propsRef.current = props;
28
29 // Cache keys since React rerender may cause it lost
30 var keysRef = React.useRef([]);
31 if (keys.length) {
32 keysRef.current = keys;
33 }
34 React.useDebugValue(timesRef.current);
35 React.useDebugValue(keysRef.current.join(', '));
36 if (debug) {
37 console.log("".concat(debug, ":"), timesRef.current, keysRef.current);
38 }
39 return timesRef.current;
40}
41var _default = exports.default = process.env.NODE_ENV !== 'production' ? useRenderTimes : function () {};
42var RenderBlock = exports.RenderBlock = /*#__PURE__*/React.memo(function () {
43 var times = useRenderTimes();
44 return /*#__PURE__*/React.createElement("h1", null, "Render Times: ", times);
45});
46if (process.env.NODE_ENV !== 'production') {
47 RenderBlock.displayName = 'RenderBlock';
48}
\No newline at end of file