Skip to content

Memoize opts object in ZeroProvider to prevent unnecessary reconnects #1097

@MODSetter

Description

@MODSetter

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

  • opts is memoized with useMemo
  • context and auth are stable references
  • Zero sync still works correctly

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions