antd
Version:
An enterprise-class UI design language and React components implementation
25 lines • 1.02 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
export default function usePatchElement() {
var _React$useState = React.useState([]),
_React$useState2 = _slicedToArray(_React$useState, 2),
elements = _React$useState2[0],
setElements = _React$useState2[1];
var patchElement = React.useCallback(function (element) {
// append a new element to elements (and create a new ref)
setElements(function (originElements) {
return [].concat(_toConsumableArray(originElements), [element]);
});
// return a function that removes the new element out of elements (and create a new ref)
// it works a little like useEffect
return function () {
setElements(function (originElements) {
return originElements.filter(function (ele) {
return ele !== element;
});
});
};
}, []);
return [elements, patchElement];
}