Skip to content

fix: useTracker should only render once on mount (closes #439)#482

Open
nicolv wants to merge 2 commits intometeor:masterfrom
nicolv:fix/useTracker-double-render
Open

fix: useTracker should only render once on mount (closes #439)#482
nicolv wants to merge 2 commits intometeor:masterfrom
nicolv:fix/useTracker-double-render

Conversation

@nicolv
Copy link
Copy Markdown

@nicolv nicolv commented Apr 28, 2026

Summary

Closes #439 - useTracker renders two times at mount

Problem

The previous implementation of useTrackerNoDeps would:

  1. Stop and recreate the Tracker computation on every render
  2. Combined with React 18 StrictMode double-rendering, this caused the component to render more times than expected

Solution

  1. Added useSyncEffect hook - A custom hook that runs effects synchronously during render but defers cleanup by 1 second to avoid StrictMode double-rendering issues

  2. Simplified useTrackerNoDeps - Now delegates to useTrackerWithDeps instead of having its own complex render-phase logic

  3. Updated useTrackerWithDeps - Uses useSyncEffect to manage the Tracker computation lifecycle, avoiding the stop/recreate on every render

Changes

  • packages/react-meteor-data/useSyncEffect.ts - New file: the useSyncEffect hook
  • packages/react-meteor-data/useTracker.ts - Simplified to use useSyncEffect

Key Technical Points

  • The previous code used Meteor.defer() and render-phase computation creation, which caused issues with StrictMode
  • useSyncEffect runs the effect in useMemo (during render) but delays cleanup by 1 second, avoiding StrictMode double-render issues
  • This is the same approach as PR [WIP] simpler useTracker #436 but refined

Testing

The existing noDepsTester test in useTracker.tests.js already covers StrictMode behavior.


Related to #439

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useTracker renders two times at mount, would be nice if it renders only once

1 participant