usePersistence
Manages whether a visitor has already seen a popup, using localStorage as the backing store. Designed to be paired with any trigger hook to prevent repeat interruptions on return visits.
$import { usePersistence } from react-marketing-popups
Signature
usePersistence(key: string)
Returns
{ hasSeen: () => boolean, markSeen: () => void, clear: () => void }Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| key | string | — | The localStorage key used to record the seen state. Must be unique per popup. |
Returns
hasSeen()Returns
true if the key is present in localStorage — i.e., the popup has been shown to this visitor before.markSeen()Writes the key to localStorage. Call this when the popup first displays so future visits skip it.
clear()Removes the key from localStorage. Useful during development to reset to a first-visit state without opening DevTools.
Code Examples
By design the open state is handled outside of the component
Basic usage
Pair usePersistence with any trigger hook. Gate open with hasSeen() and call markSeen via onClose so it records on first dismissal.
Loading...Cooldown — show again after N days
usePersistence uses a simple boolean flag (key present in localStorage = seen). For time-based cooldowns, store a timestamp instead.
Loading...Development reset with clear()
During development, call clear() to wipe the localStorage key so you can preview the first-visit experience.
Loading...Multiple popups with separate keys
Each popup must use a unique key. usePersistence is lightweight — you can call it multiple times.
Loading...