Skip to content

Setting state within useTracker causes effects to run reactively #317

@edemaine

Description

@edemaine

Consider the following code:

defaultPage = new ReactiveVar();
function App() {
  [page, setPage] = useState();
  useEffect(() => {
    console.log(Tracker.currentComputation);
    const observer = Objects.find({page: page}).observe({added: ...});
    return () => observer.stop();
  }, [page]);
  useTracker(() => {
    if (!page) {
      setPage(defaultPage.get());
    }
  }, [!page]);
  ...
}

I find that the useEffect callback is run within a Tracker (the console.log outputs an actual computation), presumably triggered by setPage called within useTracker. I tried wrapping setPage within Tracker.nonreactive, but the effect callback is still called reactively. This seems wrong...

The result of this is pretty annoying. The tracker actually gets called twice, and because of the if, only the first one triggers the observer to be created. Because the observer was created within a tracker, the second call of the tracker invalidates the first call so the observer gets destroyed and not recreated. 🙁

I tested in react-meteor-data 2.1.4, 2.2.1, and 2.2.2 (wondering whether the recent rewrite had an effect) and both have the same issue. I'm using React 17.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    in-discussionWe are still discussing how to solve or implement it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions