persist-ui
Version:
Universal input persistence for Vanilla, React, Vue, Astro, Next.js etc. with secure AES encryption.
22 lines (21 loc) • 579 B
JavaScript
import { useRef, useEffect } from "react";
import { persistUI } from "./core";
export function usePersistUI(key, opts) {
// Support all form element types
const ref = useRef(null);
useEffect(() => {
if (!ref.current)
return;
const persist = persistUI.attach(ref.current, {
key,
...opts
});
// Only reset on unmount if explicitly requested
return () => {
if (opts?.resetOnUnmount) {
persist.reset();
}
};
}, [key, opts]);
return ref;
}