Skip to content

hannojg/react-native-firebase-hooks

Repository files navigation

🔥⚓️ react-native-firebase-hooks

React Hooks for React Native Firebase

Installation

yarn add @skillnation/react-native-firebase-hooks

Note: This assumes you have setup react-native-firebase in your project.

Usage

Example

import React from 'react';
import type { Product } from './types';
import firestore from '@react-native-firebase/firestore';
import { useCollectionData } from '@skillnation/react-native-firebase-hooks/firestore';

export const App: React.FC = () => {
  const [products, isLoading, error] = useCollectionData(
    firestore().collection<Product>('products')
  );

  if (isLoading) return <LoadingComponent />
  if (error) return <ErrorComponent error={error} />
  return <ProductList products={products} />;
};

Documentation

⚠️ These doc links redirect to react-firebase-hooks.

The API is exactly the same as react-firebase-hooks. Only the imports are different:

- import {} from 'react-firebase-hooks/MODULE_NAME'
+ import {} from '@skillnation/react-native-firebase-hooks/MODULE_NAME'

Why use this instead of react-firebase-hooks?

react-firebase-hooks is incompatible with the latest versions of react-native-firebase, especially from a types perspective. This library works internally mostly the same as react-firebase-hooks. So you really only want to use this lib for react-native if you are using react-native-firebase.

Migrating from react-firebase-hooks

The only thing you have to do is to change the imports:

- import {} from 'react-firebase-hooks/MODULE_NAME'
+ import {} from '@skillnation/react-native-firebase-hooks/MODULE_NAME'

Note however, that this library is based on react-firebase-hooks v4, as this version was for the Firebase JS SDK v8, which had a very similar API to the native SDKs. We do include features of newer versions of react-firebase-hooks (such as the reload() callback on use*Once hooks, which was added in v5).

However, any change in newer version that is for compatibility with Firebase Js SDJ v9 won't be added to this library. This is due to the fact that the firebase JS SDK has a different API from the native SDKs, so we can't have 100% feature parity.

As an example FirestoreDataConverter is something that just exists in the Firebase v9 JS SDK, and can't be added here.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT