Skip to content

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

Open

Description

@Poyoman39

This code renders two times

    const Test = ({ afterMountInc = false }) => {
      renderCount++;
      console.log(renderCount);
      value = useTracker(() => {
        return null;
      }, []);
      return <span>{renderCount}</span>;
    };

    ReactDOM.render(<Test />, container);

Activity

  1. Poyoman39 commented on Apr 14, 2025

    @Poyoman39
    ContributorAuthor

    This PR #436 intends to fix this issue

  2. Poyoman39 commented on Apr 14, 2025

    @Poyoman39
    ContributorAuthor

    This PR #440 intends to add a test for this issue

  3. linked a pull request that will close this issue[WIP] simpler useTracker #436on Apr 14, 2025
  4. nachocodoner commented on Apr 17, 2025

    @nachocodoner
    Member

    Is this related with useSubscribe as well as reported in the forums https://forums.meteor.com/t/usesubscribe-and-usetracker-fire-multiple-times-react-meteor-data/63448/4?

    I wonder if latest fixes on master have fixed it, likely not yet. Last published version, meteor add react-meteor-data@3.0.5-beta.0

  5. Poyoman39 commented on Apr 17, 2025

    @Poyoman39
    ContributorAuthor

    Not sure 🤔 I can write a test to confirm this if you want @nachocodoner

  6. nachocodoner commented on Apr 17, 2025

    @nachocodoner
    Member

    That'd be great @Poyoman39

  7. added a commit that references this issue on Apr 28, 2026
    a261e22
  8. nicolv commented on Apr 28, 2026

    @nicolv

    I've submitted a PR to fix this issue: #482

    The fix is based on the approach from #436 (simpler useTracker with useSyncEffect) but refined:

    • Added useSyncEffect hook that runs effects synchronously during render but defers cleanup by 1 second to avoid StrictMode double-rendering issues
    • Simplified useTrackerNoDeps to delegate to useTrackerWithDeps
    • Updated useTrackerWithDeps to use useSyncEffect for managing the Tracker computation lifecycle

    The root cause was that the old useTrackerNoDeps would stop and recreate the computation on every render, which combined with StrictMode caused extra renders.

    Feedback welcome!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions