Cleanup and Lifecycle
EdgeInteract React hooks are designed to be safe — subscriptions are cleaned up automatically when components unmount or when hook parameters change. This page explains how the cleanup works and what to watch out for in more advanced scenarios.
Automatic Cleanup Summary
| Hook / Component | What Gets Cleaned Up | When |
|---|---|---|
InteractionProvider | interactions.{userId} subscription, dismiss subscription | On unmount or user ID change |
useInteractionReceiver() | Reads from shared context — no subscription leak possible | — |
useInteractionCallback(id) | interactions.ack.{id} subscription | On response received, unmount, or id change |
useInteraction() | If the component unmounts while awaiting, the Promise is cancelled | On unmount |
Unmount Safety
If a component that called useInteraction() unmounts before the response arrives (e.g., the user navigates away), the hook automatically cancels the pending interaction. The promise rejection is swallowed internally — React's "Can't perform a state update on an unmounted component" warning will not appear.
// This is safe — even if the user navigates away before the manager responds
function ApprovalButton() {
const { sendInteraction } = useInteraction();
const [status, setStatus] = useState('idle');
const handleSend = async () => {
setStatus('pending');
try {
const response = await sendInteraction({ ... });
setStatus('complete'); // Only runs if still mounted
showToast(response.outcome);
} catch (err) {
if (!isUnmountError(err)) {
setStatus('error'); // Only runs if still mounted
}
}
};
// ...
}
User ID Change
If the user logs out and a new user logs in within the same app session, InteractionProvider detects the user ID change (from EdgeStreamProvider) and:
- Unsubscribes from the previous user's interaction topics
- Clears the interaction queue
- Subscribes to the new user's interaction topics
No manual cleanup is required on your end.
React Strict Mode
EdgeInteract hooks are compatible with React Strict Mode. In Strict Mode, effects run twice in development. The subscription setup/teardown cycle handles this correctly — double subscriptions are deduplicated by the EdgeStream client.
InteractionProvider, InteractionContainer, and the provided hooks, you will never need to write manual cleanup code. The library handles everything.