useFind not re-finding when query changes

Hello all. Been building my first Meteor app for a few months now using MaterialUI and ReactJS. It’s coming along well!

Up until now I’ve just been using useTracker with the normal Collection.find().fetch() from the documentation. Everything is working fine.

Today I decided to try the “useFind” capability from react-data-hooks so I could get some of that fine-grained updating goodness. However, useFind seems to not update the query when I change the value of the arguments. It will only update it if I change the names of the arguments.

Example:

useFind(() => MyCollection.find({stuff: 4})) // Works perfectly

// Sometime later after the query needs to change
useFind(() => MyCollection.find({stuff: 7})) // Does NOT change the query results!

// Sometime later after the query needs to change again
useFind(() => MyCollection.find({other_stuff: 12})) // Will then work perfectly again!

So: what I’m seeing is that calling useFind is not updating the query results unless the name of the query results changes.

Any idea what’s going on here?

One more piece of info: the actual query JSON is built before calling useFind (just above it)… could that have anything to do with it?

1 Like

Nevermind - I got it. I needed to pass the dependencies of the calculation that was creating the query so that when they changed useFind would know that it needs to re-find.

For anyone that comes across this same issue, I needed to do:

result = useFind(() => MyCollection.find(query), [dep1, dep2, dep3])

Where “dep1”, “dep2”, “dep3” are variables that go into the algorithm to decide what “query” is.

Now it’s all good.

1 Like

Hi @friedmud! Welcome to the community! :).

What you’ve find is the common use case for many hooks: useCallback, useMemo, useEffect, for example, all use the same dependency list to help decide whether they need to be updated/re-run or not.

BTW: I’m not sure if this is still a common practice in the forum, but some users decide to change their question subject adding a [solved] prefix so that it’s clear that help is no longer required and an answer is in place if you’re looking for one.

Best regards!