Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

anstapol
Copy link

@anstapol anstapol commented Jul 18, 2024

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:

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.

Resolves #2651

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);
Copy link

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.

Copy link
Author

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?

Copy link
Author

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.

https://playcode.io/1942093

Copy link

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

Copy link

@rphlmr rphlmr left a 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.

@AndriiSherman
Copy link
Member

@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?

@burtek
Copy link

burtek commented Jul 31, 2024

@AndriiSherman any chance to merge and release this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useLiveQuery doesn't update if the query parameters change.
4 participants