usePatchElement.js 708 B

123456789101112131415
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import * as React from 'react';
  3. export default function usePatchElement() {
  4. const [elements, setElements] = React.useState([]);
  5. const patchElement = React.useCallback(element => {
  6. // append a new element to elements (and create a new ref)
  7. setElements(originElements => [].concat(_toConsumableArray(originElements), [element]));
  8. // return a function that removes the new element out of elements (and create a new ref)
  9. // it works a little like useEffect
  10. return () => {
  11. setElements(originElements => originElements.filter(ele => ele !== element));
  12. };
  13. }, []);
  14. return [elements, patchElement];
  15. }