ant-design/components/_util/hooks/usePatchElement.ts
lijianan 0b6356d984
chore: rename file .tsx => .ts (#46759)
* chore: rename file .tsx => .ts

* fix: fix

* test: add test case
2024-01-03 08:45:11 +08:00

22 lines
730 B
TypeScript

import * as React from 'react';
export default function usePatchElement(): [
React.ReactElement[],
(element: React.ReactElement) => Function,
] {
const [elements, setElements] = React.useState<React.ReactElement[]>([]);
const patchElement = React.useCallback((element: React.ReactElement) => {
// append a new element to elements (and create a new ref)
setElements((originElements) => [...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 () => {
setElements((originElements) => originElements.filter((ele) => ele !== element));
};
}, []);
return [elements, patchElement];
}