How To Add Data To Firebase Firestore Database Using React Native App | React Native Tutorial | JS
Table of Contents
Introduction
In this tutorial, you will learn how to add data to a Firebase Firestore database using a React Native app built with Expo. This process will guide you through setting up Firebase, creating a React Native application, and uploading text data to Firestore. By the end, you will have a functional mobile application that interacts with your Firebase database.
Step 1: Set Up a Firebase Application
-
Create a Firebase Project
- Go to the Firebase Console.
- Click on "Add project" and follow the prompts to create a new project.
-
Enable Firestore Database
- In your Firebase project, navigate to "Firestore Database" from the sidebar.
- Click "Create database" and choose the appropriate security rules for your needs.
-
Get Firebase Configuration
- After setting up Firestore, go to "Project settings".
- Under the "General" tab, find your Firebase configuration. You will need this for your React Native app.
Step 2: Create a React Native Expo Application
-
Install Expo CLI
- If you haven't already installed Expo CLI, run the following command in your terminal:
npm install -g expo-cli
- If you haven't already installed Expo CLI, run the following command in your terminal:
-
Initialize a New Project
- Create a new Expo project by running:
expo init MyFirebaseApp
- Choose a template (e.g., blank) and navigate into the project folder:
cd MyFirebaseApp
- Create a new Expo project by running:
Step 3: Install Firebase SDK
-
Add Firebase to Your Project
- Install the Firebase SDK by running:
npm install firebase
- Install the Firebase SDK by running:
-
Import Firebase in Your App
- Open your
App.js
file and import Firebase:import { initializeApp } from 'firebase/app'; import { getFirestore } from 'firebase/firestore';
- Open your
-
Initialize Firebase
- Add your Firebase configuration and initialize Firebase:
const firebaseConfig = { // Your Firebase configuration }; const app = initializeApp(firebaseConfig); const db = getFirestore(app);
- Add your Firebase configuration and initialize Firebase:
Step 4: Create a Minimal UI for Data Input
-
Set Up State Management
- Use the
useState
hook to manage input data:import React, { useState } from 'react';
- Use the
-
Create Input Fields
- In the return statement of your component, create a simple form:
const [inputData, setInputData] = useState(''); return ( <View> <TextInput placeholder="Enter data" value={inputData} onChangeText={setInputData} /> <Button title="Submit" onPress={addDataToFirestore} /> </View> );
- In the return statement of your component, create a simple form:
Step 5: Add Data to Firestore
- Define the Function to Upload Data
- Create a function to handle the button press and add data to Firestore:
import { collection, addDoc } from 'firebase/firestore'; const addDataToFirestore = async () => { try { await addDoc(collection(db, 'your-collection-name'), { data: inputData, timestamp: new Date() }); setInputData(''); // Clear the input field } catch (error) { console.error("Error adding document: ", error); } };
- Create a function to handle the button press and add data to Firestore:
Conclusion
You have successfully created a React Native app that uploads text data to a Firebase Firestore database. Key steps included setting up Firebase, creating a new Expo project, installing the Firebase SDK, creating a minimal user interface, and implementing the functionality to add data to Firestore. Now you can expand your app further by adding features such as data retrieval, user authentication, and more complex UI elements. Happy coding!