Skip to content

Commit eecab69

Browse files
committed
run the callback non-reactively in the initial render
1 parent f9c6ae3 commit eecab69

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

packages/react-meteor-data/useTracker.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ if (Meteor.isServer) {
1111
}
1212
else {
1313
useTracker = (reactiveFn, dependencies) => {
14-
const [trackerData, setTrackerData] = useState(null);
1514
const callback = useCallback(reactiveFn, dependencies);
1615

16+
// Run the function once with no autorun to get the initial return value.
17+
// @todo Reach out to the React team to see if there's a better way ? Maybe abort the initial render instead ?
18+
const [trackerData, setTrackerData] = useState(() => {
19+
// We need to prevent subscriptions from running in that initial run.
20+
const realSubscribe = Meteor.subscribe;
21+
Meteor.subscribe = () => ({ stop: () => {}, ready: () => false });
22+
const initialData = Tracker.nonreactive(callback);
23+
Meteor.subscribe = realSubscribe;
24+
return initialData;
25+
});
26+
1727
useEffect(() => {
1828
let computation;
1929
// Use Tracker.nonreactive in case we are inside a Tracker Computation.

packages/react-meteor-data/withTracker.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function withTracker(options) {
2121
});
2222
}
2323

24-
return data ? <Component {...{ ...props, ...data }} /> : null;
24+
return <Component {...{ ...props, ...data }} />;
2525
}
2626

2727
return pure ? memo(WithTracker) : WithTracker;

0 commit comments

Comments
 (0)