Usage

Components

  • FirebaseDatabaseProvider

  • FirebaseDatabaseNode

  • FirebaseDatabaseMutation

  • FirebaseDatabaseTransaction

Place a FirebaseDatabaseProvider component at the top level of your app (anywhere as long as it's above the other Realtime Database components).

import { FirebaseDatabaseProvider } from '@react-firebase/database'
// Before
const App = () => {
    return <div>
        This is my app
    </div>
}

// After
const App = () => {
    return (
        <FirebaseDatabaseProvider>
            <div>
                This is my app
            </div>
        </FirebaseDatabaseProvider>
    )
}

If you need to authenticate to access your data, check out @react-firebase/auth

Firebase Database Node

Check API for a list of supported props.

Read data from Firebase example

Firebase Database Mutation

The FirebaseDatabaseMutation allows you to render components that run Firebase mutations (update, set, push).

A setMutation function that returns a promise is provided to the children function.

FirebaseDatabaseMutation needs 3 props :

  • path: string

  • operation: "update" | "set" | "push"

  • children:

    ( { runMutation: (value:any) => Promise<{key?:string}> } ) => ReactNode

Write data to Firebase example

Firebase Database Transaction

Suppose you have a shared counter that many can increment. To avoid race conditions, use transactions.

FirebaseDatabaseTransaction needs 2 props:

  • path: string

  • children:

    ( { runTransaction: ({ reducer: (value:any)=>any }) => Promise<{key?:string}> } ) => ReactNode

Firebase Transaction Example

Complete Example

Last updated