From 8b2c4a38a461ff5ecc95972291bc711e2c5dec9a Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 28 Apr 2023 01:47:57 +0800 Subject: --- .../packages/acme-utils/src/usePrevious.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 examples/with-changesets/packages/acme-utils/src/usePrevious.tsx (limited to 'examples/with-changesets/packages/acme-utils/src/usePrevious.tsx') diff --git a/examples/with-changesets/packages/acme-utils/src/usePrevious.tsx b/examples/with-changesets/packages/acme-utils/src/usePrevious.tsx new file mode 100644 index 0000000..8024464 --- /dev/null +++ b/examples/with-changesets/packages/acme-utils/src/usePrevious.tsx @@ -0,0 +1,17 @@ +import * as React from "react"; + +function usePrevious(value: T) { + // The ref object is a generic container whose current property is mutable ... + // ... and can hold any value, similar to an instance property on a class + const ref = React.useRef(value); + + // Store current value in ref + React.useEffect(() => { + ref.current = value; + }, [value]); // Only re-run if value changes + + // Return previous value (happens before update in useEffect above) + return ref.current; +} + +export { usePrevious }; -- cgit v1.2.3-70-g09d2