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
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of
10 min read
World Wide Web (WWW) The World Wide Web (WWW), often called the Web, is a system of interconnected webpages and information that you can access using the Internet. It was created to help people share and find information easily, using links that connect different pages together. The Web allows us to browse websites, wat
6 min read
React Introduction ReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.It is developed and maintained by Facebook.The latest version of React is React 19.Uses
8 min read
Client-Server Model The Client-Server Model is a distributed application architecture that divides tasks or workloads between servers (providers of resources or services) and clients (requesters of those services). In this model, a client sends a request to a server for data, which is typically processed on the server
6 min read
JavaScript Arrays In JavaScript, an array is an ordered list of values. Each value is called an element, and each element has a numeric position in the array, known as its index. Arrays in JavaScript are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.Array in JavaScriptWhy Use
7 min read
Axios in React: A Guide for Beginners React is one of the most popular JavaScript libraries for building user interfaces, and it works seamlessly with various data-fetching tools. Among these tools, Axios stands out as one of the easiest and most efficient libraries for making HTTP requests in React.In this article, weâll explore how to
6 min read
Difference between HTTP GET and POST Methods HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET
4 min read
What is the Information Technology Act, 2000 (IT Act)? The Information Technology Act, 2000, also known as the IT Act, was passed by the Indian Parliament on 17th October 2000. It provides the legal framework for electronic transactions, digital communication, and cybercrimes in India. This law was designed to promote e-governance, regulate online trans
4 min read
Flask Rendering Templates Flask is a lightweight Python web framework that enables developers to build web applications easily. One of its key features is template rendering, which allows dynamic content generation using Jinja2 templating. In this guide, we'll explore how to render templates in Flask.Setting up FlaskSetting
6 min read