-
-
Notifications
You must be signed in to change notification settings - Fork 742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: forwaring dependencies within useLiveQuery #2652
Conversation
Add support for dynamic query dependencies in hooks This update enables the use of dynamic values in query conditions within hooks, ensuring that queries are properly updated when their dependencies change. Example Using dynamic values in the where clause: ```ts const todos = useLiveQuery( db.query.todos.findMany({ where: between(schema.todos.created_at, from, to), }) ) ``` This enhancement is particularly useful for scenarios where the query depends on a set of changing dependencies, allowing for more flexible and responsive data fetching.
@@ -43,7 +44,7 @@ export const useLiveQuery = <T extends Pick<AnySQLiteSelect, '_' | 'then'> | SQL | |||
return () => { | |||
listener?.remove(); | |||
}; | |||
}, []); | |||
}, deps); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't [query]
enough?
It could be then automatically handled for everyone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't the query be referencing a new object on each re-render?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a simple example demonstrating how adding the deps
array allows for more granular control over the re-execution of a query.
If you only pass the query, it will change with every re-render of the component. However, by specifying a separate dependency, the query will update only when necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes of course it would be better!
It would avoid wrapping the query in a useCallback
before passing it to useLiveQuery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm.
What the team is thinking about this?
No breaking changes, and it allows the developer to control when to refresh useLiveQuery
.
@anstapol Thanks a lot! I'll merge this one, but could you please use the dprint formatter and push the changes so all the checks will pass? |
@AndriiSherman any chance to merge and release this? |
Done! Anything else missing? |
Add support for dynamic query dependencies in hooks
This update enables the use of dynamic values in query conditions within hooks, ensuring that queries are properly updated when their dependencies change.
Example
Using dynamic values in the where clause:
This enhancement is particularly useful for scenarios where the query depends on a set of changing dependencies, allowing for more flexible and responsive data fetching.
Resolves #2651