useMemoCallback.js 679 B

123456789101112131415161718
  1. import * as React from 'react';
  2. /**
  3. * Cache callback function that always return same ref instead.
  4. * This is used for context optimization.
  5. */
  6. export default function useMemoCallback(func) {
  7. var funRef = React.useRef(func);
  8. funRef.current = func;
  9. var callback = React.useCallback(function () {
  10. var _funRef$current;
  11. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  12. args[_key] = arguments[_key];
  13. }
  14. return (_funRef$current = funRef.current) === null || _funRef$current === void 0 ? void 0 : _funRef$current.call.apply(_funRef$current, [funRef].concat(args));
  15. }, []);
  16. return func ? callback : undefined;
  17. }