-
Notifications
You must be signed in to change notification settings - Fork 600
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
Simple usage in hooks #1848
Comments
This might help. |
Hey thanks, this is kind of the same as what I am doing. I'll try if I hope official hooks can be made available soon. |
@isaachinman I'd be interested in your criticism for those hooks. The drawback is that I have to freeze some data outside of the hook like this: const commentsFilter = useMemo(
() => [Q.where('step', stepIndex), Q.sortBy('name')],
[stepIndex],
)
const observeColums = useRef(['step', 'name']) But on the other side I guess joining column names and json-stringifying the query clauses is very fast anyway. But I am not sure what are the pro/cons over |
I am no rxjs expert. But my understanding is that
Any It's a shame we have not had a response from @radex, as I am sure we could easily put together a collaborative PR to introduce official hooks. |
Yes that would make it far easier to use, no need for the user to think about memoization. Ok I looked at the code and indeed the observable you create seems to be properly unsubscribed from when a new observable is given to Yeah, a library of hooks could be very useful. In the mean time if you have other hooks to share I'd be pretty happy. If you look at my first hook above I have to call |
The |
Oh so you define the hooks there so no need to |
Another version to avoid JSON.stringify: export function useExtendQueryColumns<T extends Model>(query: Query<T>, clause: Clause | Clause[], observeColumns: string[]): T[] {
const [observableColumnsMemo, clauseMemo] = useDeepMemo(
() => [observeColumns, arrayWrap(clause)],
[observeColumns, clause],
)
const observable = useMemo(
() => query
.extend(...clauseMemo)
.observeWithColumns(observableColumnsMemo)
,
([query, observableColumnsMemo, clauseMemo]),
)
return useObservableState(observable, [])
}
function arrayWrap<T>(itemOrList: T | T[]): T[] {
if (Array.isArray(itemOrList)) return itemOrList
return [itemOrList]
}
function useDeepMemo<T>(generator: () => T, dependencies: unknown[]): T {
const ref = useRef({ value: null, deps: [] }) as MutableRefObject<{ value: null | T, deps: unknown[] }>
if (!isEqual(ref.current.deps, dependencies)) {
ref.current = { value: generator(), deps: dependencies }
}
return ref.current.value as T
} Not sure how to benchmark that though. |
Hello,
I would like your advice on how to use WDB from hooks. I know queries hooks are not supported right now, but I have seen examples on the web that I can't find again. I guess it would be nice to add them as temporary solutions somewhere.
I would like to know if there is a way to simplify the following:
Subscribe to a query
Query from a "has many" relationship
I now have to add variants for
observeWithColumns
.Thank you.
edit Version with columns from a relationship
The text was updated successfully, but these errors were encountered: