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

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion drizzle-orm/src/expo-sqlite/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SQLiteRelationalQuery } from '~/sqlite-core/query-builders/query.ts';

export const useLiveQuery = <T extends Pick<AnySQLiteSelect, '_' | 'then'> | SQLiteRelationalQuery<'sync', unknown>>(
query: T,
deps: unknown[] = []
) => {
const [data, setData] = useState<Awaited<T>>(
(is(query, SQLiteRelationalQuery) && query.mode === 'first' ? undefined : []) as Awaited<T>,
Expand Down Expand Up @@ -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
Contributor 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
Contributor 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


return {
data,
Expand Down
Loading