How to push data into firebase Realtime Database using ReactJS ?
Last Updated :
26 Jul, 2024
Firebase is a popular backend-as-a-service platform that provides various services for building web and mobile applications. One of its key features is the Realtime Database, which allows developers to store and sync data in real-time. In this article, we will explore how to push data into the Firebase Realtime Database using ReactJS.
The following approach covers how to push data into firebase's real-time database using react. We have used the firebase module to achieve so.
Creating React Application And Installing Module:
Step 1: Create a React-app using the following command:
npx create-react-app myapp
Step 2: After creating your project folder i.e. myapp, move to it using the following command:
cd myapp
Project structure: Our project structure will look like this.

Step 3: After creating the ReactJS application, Install the firebase module using the following command:
npm install [email protected] --save
Step 4: Go to your firebase dashboard and create a new project and copy your credentials.
const firebaseConfig = {
apiKey: "your api key",
authDomain: "your credentials",
projectId: "your credentials",
storageBucket: "your credentials",
messagingSenderId: "your credentials",
appId: "your credentials"
};
Step 5: Initialize the Firebase into your project by creating firebase.js file with the following code.
JavaScript
import firebase from 'firebase';
const firebaseConfig = {
// Your Credentials
};
firebase.initializeApp(firebaseConfig);
var database = firebase.database();
export default database;
Step 6: Now go to your Realtime Database section in the firebase project and update your security rules. Here we are in testing mode, so we allow both read and write as true. After updating the code shown below, click on publish.

Step 7: Now implement the main part. Here, We are going to use a method called set which pushes data to our real-time database.
App.js
JavaScript
import { useState } from 'react';
import database from './firebase';
function App() {
const [name, setName] = useState();
const [age, setAge] = useState();
// Push Function
const Push = () => {
database.ref("user").set({
name: name,
age: age,
}).catch(alert);
}
return (
<div className="App" style={{ marginTop: 250 }}>
<center>
<input placeholder="Enter your name" value={name}
onChange={(e) => setName(e.target.value)} />
<br /><br />
<input placeholder="Enter your age" value={age}
onChange={(e) => setAge(e.target.value)} />
<br /><br />
<button onClick={Push}>PUSH</button>
</center>
</div>
);
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Similar Reads
How to get current date and time in firebase using ReactJS ? This article provides a detailed walkthrough on obtaining the current date and time in a Firebase environment using ReactJS. Readers will gain insights into the implementation process, enabling date and time functionalities into their React projects powered by Firebase.Prerequisites:Node JS or NPMRe
2 min read
How to get meta data of files in firebase storage using ReactJS ? Within the domain of web development, effective file management is a frequent necessity, and Firebase Storage emerges as a resilient solution. This article explores the nuances of extracting metadata from files housed in Firebase Storage through the lens of ReactJS. PrerequisitesNode JS or NPMReact
2 min read
How to use Firestore Database in ReactJS ? Firebase is a comprehensive backend solution offered by Google that simplifies the process of building, managing, and growing applications. When developing mobile or web apps, handling the database, hosting, and authentication can be challenging tasks. Firebase addresses these challenges by providin
9 min read
Firebase Realtime Database: Reading and Writing Data Firebase Realtime Database, a cloud-hosted NoSQL database developed by Google, provides a robust solution for achieving seamless real-time data updates across connected clients. In this article, We will learn about the Firebase Realtime Database, How to Setting Up the Firebase Realtime Database, wri
7 min read
Data Organization in Firebase Realtime Database Firebase Realtime Database is a powerful tool that allows us to store and synchronize data in real-time across all clients. However, organizing data effectively in Firebase Realtime Database can be challenging for beginners. Proper data organization is important for efficient data retrieval, minimiz
7 min read
How to authenticate phone number using firebase in ReactJS ? User authentication is a critical aspect of many web applications, and phone number authentication has become increasingly popular for its convenience and security. Firebase, a powerful platform developed by Google, offers various authentication methods, including phone number authentication. Prereq
3 min read
Integrating Real-time Database and Authentication using Next JS and Firebase NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn how to integrate Firebase's real-time database
5 min read
How to list all the files from firebase storage using ReactJS ? To list all the files from Firebase storage using React JS we will access the Firebase storage using the SDK and display the data on the webpage from the storage.Prerequisites:NPM & Node.jsReact JSFirebaseApproach: To list all the files from Firebase storage using ReactJS we will first configure
3 min read
How to Download Firebase Realtime Database Data in Excel File? Firebase is a product of Google which helps developers to build, manage, and grow their apps easily. It helps developers to build their apps faster and in a more secure way. We require No programming on the firebase side which makes it easy to use its features more efficiently. It provides services
2 min read
How to upload files in firebase storage using ReactJS ? Firebase Storage is a powerful cloud storage solution provided by Google's Firebase platform. It allows developers to store and retrieve user-generated content, such as images, videos, and other files, in a secure and scalable manner. In this article, we will explore how to upload files to Firebase
2 min read