Post Data to Firebase using Axios

Post Data to Firebase using Axios




How to create Firebase database

  1. Goto https://firebase.google.com/
  2. Login with google account
  3. Click on Go to console button on the top right side


  4. Click on Add Project button to create a database


  5. Give any name and click on continue


  6. Disable google analytics and click on create project


  7. Once project is created in menu navigate to Build > Realtime Database


  8. After navigating to Realtime Database, Click on create database button


  9. Let it to default location United States and click on next


  10. Select Test Mode and click on enable


  11. Goto Rules tab to edit read and write rules, Set read and write rules to true then click on publish


  12. Copy the url from data tab and try with below code to post data to created database. Refer below image for URL Location.


  13. Add your firebase database URL to below <YourFirebaseDatabaseURL>
    https://<YourFirebaseDatabaseURL>/register.json
Install Axios Library with below npm command
npm install axios


import React, { useEffect, useState } from "react";
import axios from "axios";

const Display = (props) => {
  const [data, seteData] = useState({
    fullname: "",
    email: "",
    password: "",
    confirmPassword: "",
  });
  // Destructring
  const { fullname, email, password, confirmPassword } = data;
  const changeHandler = (e) => {
    seteData({ ...data, [e.target.name]: [e.target.value] });
  };
  const submitHandler = (e) => {
    e.preventDefault();
    axios.post("https://<YourFirebaseDatabaseURL>/register.json"
      , data).then(()=> alert('Data Submitted Successfully!'));
  };
  return (
    <div style={{ margin: "20px" }}>
      <form onSubmit={submitHandler}>
        <input
          type="text"
          name="fullname"
          value={fullname}
          onChange={changeHandler}
          placeholder="Enter Name"
        />
        <br />
        <input
          type="email"
          name="email"
          value={email}
          onChange={changeHandler}
          placeholder="Enter Email"
        />
        <br />
        <input
          type="password"
          name="password"
          value={password}
          onChange={changeHandler}
          placeholder="Enter Password"
        />
        <br />
        <input
          type="password"
          name="confirmPassword"
          value={confirmPassword}
          onChange={changeHandler}
          placeholder="Confirm Password"
        />
        <br />
        <input type="submit" name="Submit" />
      </form>
    </div>
  );
};

export default Display;


Raviteja Mulukuntla

Hello Viewers! My name is RaviTeja Mulukuntla and I am a senior software engineer working in a reputed IT firm. I like sharing my professional experience and knowledge skills with all users across all the Digital Platforms.

Previous Post Next Post

Contact Form