-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Memoize opts object in ZeroProvider to prevent unnecessary reconnects #1097
Copy link
Copy link
Open
Labels
Description
Description
In ZeroProvider.tsx, the opts object is recreated on every render and spread into ZeroReactProvider. If the provider compares props by reference internally, this can cause extra reconnects or internal churn on every parent re-render.
File to change
surfsense_web/components/providers/ZeroProvider.tsx(lines 51-61)
Current code
const opts = {
userID,
schema,
queries,
context,
cacheURL,
auth,
};
return (
<ZeroReactProvider {...opts}>What to do
const opts = useMemo(
() => ({ userID, schema, queries, context, cacheURL, auth }),
[userID, schema, queries, context, cacheURL, auth]
);
return (
<ZeroReactProvider {...opts}>Also verify that context and auth values are stable references (not recreated every render). If they are, memoize those too:
const context = useMemo(
() => (hasUser ? { userId: String(user.id) } : undefined),
[hasUser, user?.id]
);Acceptance criteria
optsis memoized withuseMemocontextandauthare stable references- Zero sync still works correctly
Reactions are currently unavailable