Here we’re providing a list of solutions to fix the “Can’t find variable: ref” error when using Firebase with React Native, ordered by relevance for current development practices.

Solution 1:

For React Native specific Firebase integration, use the official React Native Firebase library (recommended for all modern apps):

// Install the package: npm install @react-native-firebase/app
// npm install @react-native-firebase/database
import database from '@react-native-firebase/database';

// Reference
const postRef = database().ref('posts');

// Push a new post
const newPostRef = postRef.push();
newPostRef.set({
 content: 'Post content'
});

This is the recommended modern approach using the official React Native Firebase SDK. It provides:

  • Better native integration with React Native
  • Improved performance and reliability
  • TypeScript support out of the box
  • Automatic error handling for React Native specific issues

Solution 2:

If you’re using a newer version of Firebase (v9+), use the modular API with specific imports:

// For Firebase v9+
import { initializeApp } from 'firebase/app';
import { getDatabase, ref, push, set } from 'firebase/database';

// Initialize Firebase
const firebaseConfig = {
 apiKey: "YOUR_API_KEY",
 authDomain: "your-app.firebaseapp.com",
 databaseURL: "https://your-app.firebaseio.com",
 storageBucket: "your-app.appspot.com",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// Get database instance
const database = getDatabase(app);

// Create a reference
const postsRef = ref(database, 'posts');

// Push new content
const newPostRef = push(postsRef);
set(newPostRef, { content: "Your post content" });

Firebase v9+ introduced a modular API that:

  • Reduces bundle size through tree-shaking(Removing unused imports and functions)
  • Provides better type safety
  • Uses more explicit function calls
  • Improves code completion in IDEs

Solution 3:

For Firebase v8 or earlier, ensure you use firebase.database().ref() to properly access the database reference:

import * as firebase from 'firebase/app';
import 'firebase/database';

// Initialize Firebase first with your config
const firebaseConfig = {
 apiKey: "YOUR_API_KEY",
 authDomain: "your-app.firebaseapp.com",
 databaseURL: "https://your-app.firebaseio.com",
 storageBucket: "your-app.appspot.com",
};
firebase.initializeApp(firebaseConfig);

// Then access ref through the database method
const postRef = firebase.database().ref('posts');

// Now you can use postRef
const newPostRef = postRef.push();

This is the traditional approach that fixes the original Stack Overflow issue by:

  • Properly initialising Firebase before use
  • Accessing ref through the database() method
  • Using the correct import statements for older Firebase versions

Conclusion:

The “Can’t find variable: ref” error typically occurs when trying to use the ref function without properly accessing it through the correct Firebase API. Here are the key points to remember:

  • For modern React Native apps, use the official @react-native-firebase library
  • For web Firebase in React Native, use the modular v9+ API with specific imports
  • Always initialize Firebase with your config before accessing any Firebase services
  • Make sure your import statements match the Firebase version you’re using
  • If using older versions, access ref through firebase.database() method
  • Ensure you have the correct Firebase packages installed in your project

Need Help With React Native Development?

Work with our skilled React Native developers to accelerate your project and boost its performance.

Hire React Native Developers

Support On Demand!

Related Q&A