Skip to content

Instantly share code, notes, and snippets.

@brennancheung
Created July 9, 2019 05:39
Show Gist options
  • Save brennancheung/a1ee4bab9ae9016c26f71c2c7e28a6bb to your computer and use it in GitHub Desktop.
Save brennancheung/a1ee4bab9ae9016c26f71c2c7e28a6bb to your computer and use it in GitHub Desktop.
export const useFirestoreCollectionQuery = (path, renderFn) => {
const [state, setState] = useState(null)
const collection = useRef(useFirestoreCollection(path))
useEffect(() => {
console.log('useEffect')
return collection.current.onSnapshot(snapshot => {
const docs = snapshot.docs.map(x => x.data())
setState(docs)
})
}, [collection])
return state === null ? <div>Loading...</div> : renderFn(state)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment