Muskan Verma
Muskan Verma

Reputation: 65

Getting deprecation warning using firebase in react native

I am using firebase in my react native project for authentication and getting this warning even after updating my code according to modular api -

This method is deprecated (as well as all React Native Firebase namespaced API) and will be removed in the next major release as part of move to match Firebase Web modular SDK API. Please see migration guide for more details: https://rnfirebase.io/migrating-to-v22 Please use where() instead.

This is my code where the warning occurs -

const checkUsernameUnique = async (userName) => {
        try {
            const usersRef = collection(db, 'Users');
            const q = query(usersRef, where('username', '==', userName));
            const snapshot = await getDocs(q);
            return snapshot.empty; // ✅ Returns true if no user found
        } catch (error) {
            console.error('Error checking username uniqueness:', error);
            return false;
        }
    };

    const checkEmailUnique = async (email) => {
        try {
            const usersRef = collection(db, 'Users');
            const q = query(usersRef, where('email', '==', email));
            const snapshot = await getDocs(q);
            return snapshot.empty;
        } catch (error) {
            console.error('Error checking email uniqueness:', error);
            return false;
        }
    };

I have checked the instruction mentioned in new documentation but still getting the warning.

Upvotes: 3

Views: 1602

Answers (2)

Osaro Imarhiagbe
Osaro Imarhiagbe

Reputation: 1

It should be

import { collection, query, where, getDocs, getFirestore } from '@react-native-firebase/firestore'

Checkout the docs: https://rnfirebase.io/migrating-to-v22

Upvotes: 0

Bilal Sabugar
Bilal Sabugar

Reputation: 41

Correct Import for Firestore (Modular API)

Instead of:

import firestore from "@react-native-firebase/firestore";

Use:

import { getFirestore, collection, query, where, getDocs } from "firebase/firestore";

If you have this anywhere in your project, remove it and switch to the modular API.

Check Your Firebase Initialization:

import { initializeApp } from "firebase/app";
import { getFirestore, initializeFirestore } from "firebase/firestore";

const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: "",
    measurementId: "",
};


export const app = initializeApp(firebaseConfig);
const db = initializeFirestore(app, {
    experimentalForceLongPolling: true
});

export default db;

Update all dependencies:

npm install firebase@latest @react-native-firebase/app @react-native-firebase/firestore

Summary:

  1. Remove any old namespaced imports
  2. Use modular API imports
  3. Ensure you're using getFirestore(app) in your Firebase setup
  4. Update Firebase SDK versions and clear Metro cache

Upvotes: -1

Related Questions