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 useFind if cursor has no value #374

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Changes from all 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
14 changes: 9 additions & 5 deletions packages/react-meteor-data/useFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,22 @@ const useFindClient = <T = any>(factory: () => (Mongo.Cursor<T> | undefined | nu
const didMount = useRef(false)

useEffect(() => {
if (!(cursor instanceof Mongo.Cursor)) {
return
}

// Fetch intitial data if cursor was changed.
if (didMount.current) {
if (!(cursor instanceof Mongo.Cursor)) {
return
}

const data = fetchData(cursor)
dispatch({ type: 'refresh', data })
} else {
didMount.current = true
}

if (!(cursor instanceof Mongo.Cursor)) {
return
}

const observer = cursor.observe({
addedAt (document, atIndex, before) {
dispatch({ type: 'addedAt', document, atIndex })
Expand All @@ -133,7 +137,7 @@ const useFindClient = <T = any>(factory: () => (Mongo.Cursor<T> | undefined | nu
}
}, [cursor])

return data
return cursor ? data : cursor
}

const useFindServer = <T = any>(factory: () => Mongo.Cursor<T> | undefined | null, deps: DependencyList) => (
Expand Down