forked from pu428f3pz/InternshipProject
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
1021 B
26 lines
1021 B
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];
|
|
} |