meteor / react-packages

Meteor packages for a great React developer experience
http://guide.meteor.com/react.html
Other
571 stars 157 forks source link

Dependent execution #407

Open StorytellerCZ opened 2 weeks ago

StorytellerCZ commented 2 weeks ago

I was recently looking at React Query and found in their docs that they have something called dependent queries: https://tanstack.com/query/latest/docs/framework/react/guides/dependent-queries

I found this very interesting and something that I often find myself needing with the hooks, especially as it relates to user based content where I don't want to trigger a subscription and everything else if the user is not logged in or at least until that data is available. This could also be used with other conditional data and could help in reducing lot of boilerplate code to handle such scenarios or wait with execution until the given data is available so that I don't have a flash of 404 page and similar issues.

radekmie commented 2 weeks ago

Do you have anything in particular in mind? For example, useSubscribe already supports it, since the name (1st argument) is optional, and there's no subscription made if it's undefined.

StorytellerCZ commented 2 weeks ago

Interesting, that can be a workaround for the subscribe:

const userId = useUserId()
useSubscribe(userId ? undefined : 'get.profile')

But even with this the useFind or useTracker that follows would still execute, so it would be nice to prevent that if the condition isn't met.

radekmie commented 2 weeks ago

useFind also supports that via undefined and null in the result of the first argument. And useTrcker can just do an early return. Sure, there’s no extra prop, but it’s clear when read (I see and use it often).

StorytellerCZ commented 2 weeks ago

I'll experiment with it a bit and then I think we can add that to the documentation or guide.