Does anyone know why useSubscribe / useTracker from the react-meteor-data package fires so many times in quick succession. Is that by design? If so, what is the benefit? If not, is that a known bug?
This is straight from the Meteor example:
// will fire multiple times
const isLoading = useSubscribe('links');
If I do something like this instead, it works as I’d expect:
How about you replace const isLoading = ... with a console.log(‘firing’). It the console log fires as often as you observe in your problem, this has nothing to do with useSubscribe / useTracker. They fire as many times they are called. It is up to you to make the rules.
The difference in your 2 examples is that in the first the function triggers every time the component re-renders while in the second example, the function only runs on the first load and it will run again when the component mounts again not when the component re-renders due to state or props updates.
Some more info for those curious and a quick reproduction.
Here’s the example straight from the Meteor 3 + React repo but with pieces commented out which seem to trigger additional renders and invocations of Meteor.subscribe.