@fluent-windows/hooks
Version:
Fluent-Windows React hooks.
52 lines (42 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var React = _interopRequireWildcard(require("react"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* React effect hook that ignores the first invocation (e.g. on mount). The signature is exactly the same as the useEffect hook.
*
* Demo
* import { useUpdateEffect } from '@fluent-windows/hooks'
*
const [count, setCount] = React.useState(0);
React.useEffect(() => {
const interval = setInterval(() => {
setCount(count => count + 1)
}, 1000)
return () => {
clearInterval(interval)
}
}, [])
useUpdateEffect(() => {
console.log('count', count) // will only show 1 and beyond
return () => {
// do something on unmount
}
})
*/
function useUpdateEffect(effect, deps) {
var isInitialMount = React.useRef(true);
React.useEffect(function () {
if (isInitialMount.current) {
isInitialMount.current = false;
} else {
return effect();
}
}, deps); // eslint-disable-line
}
var _default = useUpdateEffect;
exports["default"] = _default;