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

[Firestore] Include typescript intersection type for idField option on all use...Data hooks #50

Closed
cdoe opened this issue Nov 14, 2019 · 4 comments

Comments

@cdoe
Copy link

cdoe commented Nov 14, 2019

For Firestore collections and docs, it would be nice if each of the ...Data hooks would automatically add the idField (if provided) as a key/value to the <T> generic.

For example, the useDocumentData hook would return something like T & { idField: string } instead of just T. In this case, idField should be represented by the actual string value entered into options.idField.

Does this make sense? I feel like it should be trivial to do something like this, but couldn't quite get it to work myself. I'd be happy to make a pull request if anyone has some extra insight.

Thanks!

@cdoe cdoe changed the title [Firestore] Include intersection type for idField option on all use...Data hooks [Firestore] Include typescript intersection type for idField option on all use...Data hooks Nov 14, 2019
@maxtotheguenther
Copy link

Totally agree with @cdoe .
Currently im solving this issue with:

export default function useSnapshot<T>(query?: firestore.Query | null | undefined, options?: {
    snapshotListenOptions?: firestore.SnapshotListenOptions | undefined;
} | undefined): [IGeneric<T>[] | undefined, firestore.QuerySnapshot | undefined, boolean, Error | undefined] {
    const [value, loading, error] = useCollection(query, options)
    const model = value && value.docs.map(val => {
        const data = val.data() as T
        const model: IGeneric<T> = {
            uid: val.id,
            data
        }
        return model
    })
    return [model, value, loading, error];
`

@laurensnl
Copy link

+1 This should definitely be in there. Does anyone have any idea how to achieve this?

@chrisbianca
Copy link
Contributor

I've just been having a play around with this and have got something working which will be part of the upcoming v3 of the library. I'm aiming to have this released in the next week or 2.

For reference, the syntax is:

const [value, loading, error] = useCollectionData<MyObject, 'idFieldName', 'refFieldName'>(...)

@chrisbianca
Copy link
Contributor

This has now been released in https://github.com/CSFrequency/react-firebase-hooks/releases/tag/v3.0.1

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

No branches or pull requests

4 participants