PromoCursed/node_modules/rc-mentions/es/hooks/useEffectState.js

28 lines
810 B
JavaScript
Raw Normal View History

2024-08-20 23:25:37 +04:00
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useState, useCallback, useEffect } from 'react';
/**
* Trigger a callback on state change
*/
export default function useEffectState() {
var _useState = useState({
id: 0,
callback: null
}),
_useState2 = _slicedToArray(_useState, 2),
effectId = _useState2[0],
setEffectId = _useState2[1];
var update = useCallback(function (callback) {
setEffectId(function (_ref) {
var id = _ref.id;
return {
id: id + 1,
callback: callback
};
});
}, []);
useEffect(function () {
var _effectId$callback;
(_effectId$callback = effectId.callback) === null || _effectId$callback === void 0 || _effectId$callback.call(effectId);
}, [effectId]);
return update;
}