Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/react-meteor-data/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGELOG

* Fix data was not correct saved between renders.

## v2.2.2, 2021-01-28
* Fix lost reactivity when using deps. https://github.com/meteor/react-packages/pull/314

Expand Down
12 changes: 7 additions & 5 deletions packages/react-meteor-data/useTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ const useTrackerNoDeps = <T = any>(reactiveFn: IReactiveFn<T>) => {
}

const useTrackerWithDeps = <T = any>(reactiveFn: IReactiveFn<T>, deps: DependencyList): T => {
let [data, setData] = useState<T>();
const dataRef = useRef<T>();
const forceUpdate = useForceUpdate();

const { current: refs } = useRef({ reactiveFn });
refs.reactiveFn = reactiveFn;
Expand All @@ -114,28 +115,29 @@ const useTrackerWithDeps = <T = any>(reactiveFn: IReactiveFn<T>, deps: Dependenc
// reactive function in a computation, then stop it, to force flush cycle.
const comp = Tracker.nonreactive(
() => Tracker.autorun((c: Tracker.Computation) => {
if (c.firstRun) data = refs.reactiveFn();
if (c.firstRun) dataRef.current = refs.reactiveFn();
})
);
// To avoid creating side effects in render, stop the computation immediately
Meteor.defer(() => { comp.stop() });
if (Meteor.isDevelopment) {
checkCursor(data);
checkCursor(dataRef.current);
}
}, deps);

useEffect(() => {
const computation = Tracker.nonreactive(
() => Tracker.autorun((c) => {
setData(refs.reactiveFn(c));
dataRef.current = refs.reactiveFn(c);
forceUpdate();
})
);
return () => {
computation.stop();
}
}, deps);

return data as T;
return dataRef.current as T;
}

const useTrackerClient = <T = any>(reactiveFn: IReactiveFn<T>, deps: DependencyList = null): T =>
Expand Down