React Firebase
  • React Firebase
  • React Firebase Realtime Database
    • Setup
    • Usage
    • API
  • React Firebase Auth
    • Setup
    • Usage
    • API
  • React Firestore Database
    • Setup
    • Usage
    • API
  • Generate Firebase Data
    • Setup
    • Usage
  • Generate Firestore Data
    • Setup
    • Usage
  • Generate JSON Data
    • Setup
    • Usage
  • Guides
    • Build a React App with Firebase Auth and Realtime Database
      • Setup the development environment
      • Add React and React DOM
      • Add Firebase
      • Listen to auth
      • Adding Google and Anonymous Auth
      • Adding Data to your Realtime Database
      • Implementing the UI
      • Writing Data
      • Read Data
  • View Source
Powered by GitBook
On this page
  1. Guides
  2. Build a React App with Firebase Auth and Realtime Database

Writing Data

Writing Data to your Firebase Realtime Database

PreviousImplementing the UINextRead Data

Last updated 6 years ago

Let's start by adding a way to the user to add data to the their firebase database.

To do this we will be using the FirebaseDatabaseMutation component from @react-firebase/database.

When a user clicks on the + button we want to push data to user_bookmarks/

The data should have the following shape :

{
    link_url: string,
    link_description: string,
    created_at: timestamp,
    updated_at: timestamp
}
<FirebaseDatabaseMutation type="push" path="user_bookmarks">
  {({ runMutation }) => (
    <form
      onSubmit={ev => {
          ev.preventDefault();
          const newLink = get(this.newLinkTextFieldRef, "current.value", "");
          const newMeta = get(this.newLinkMetaTextFieldRef, "current.value", "");
          await runMutation({
            link_url: newLink,
            link_description: newMeta,
            created_at: firebase.database.ServerValue.TIMESTAMP,
            updated_at: firebase.database.ServerValue.TIMESTAMP
          });
          set(this.newLinkTextFieldRef, "current.value", "");
          set(this.newLinkMetaTextFieldRef, "current.value", "");
      }}
    >
      <input label="New Link URL" ref={this.newLinkTextFieldRef} />{" "}
      <input label="New Link Metadata" ref={this.newLinkMetaTextFieldRef} />{" "}
      <button type="submit"> + </button>
    </form>
  )}
</FirebaseDatabaseMutation>;

You can see the full code .

here
User Input UI