Jan 15, 2020

Firebase as a React hook

In a prior post, "How we use Firebase instead of Redux (with React)," I discussed how we created a withDbData function to load data from Firebase Realtime Database (RTDB) into React conveniently.

Now that we've switched to writing most of our components as functions, I wanted a hook equivalent for loading state. In this post, I'll explain how to use and how I implemented useDbDatum / useDbData, two hooks for generically loading data from Firebase RTDB.

Note: you can get the code as a gist here.

Usage

useDbDatum is a hook that loads a single datum at a single path in Firebase RTDB.

You could, for instance, use useDbDatum as follows:

const Name = ({uid}) => {
let name = useDbDatum(`users/${uid}/name`)
return <div>{name}</div>
}

Note that name is null initially, but the component re-renders with the value once it loads.

useDbData loads multiple paths at the same time, returning an object where the keys are the paths and the values are the data in Firebase RTDB.

Most of the time, you'll want to use useDbDatum over useDbData - it's more convenient and direct - but I've had to use useDbData once or twice in our code base. Most of the time, it has to do with performing ordering operations over lists, eg alphabetical sorting.

If ordering is not a requirement or a default ordering is good enough, I recommend making a subcomponent that uses useDbDatum to load its own data, rather than loading the data in the list.

An example where we need useDbData is the sorted student names class below. We load the list of student IDs from the class, using useDbDatum, then load all of the students' names using useDbData.

const SortedStudentNames = ({classUid}) => {
let students = useDbDatum(`classes/${classUid}/students`);
let uids = Object.keys(students || {});
let paths = uids.map(id => `students/${id}/name`);
let nameValues = useDbData(paths);
let names = Object.values(nameValues || {});
names.sort();
return <p>{names.join(', ')}</p>
}

Implementation

During this implementation, I learned a lot about React hooks. I found it pretty quick to get up and running with useReducer and useEffect, but the tricky key to getting useDbData working was useRef.

useRef provides an escape hatch from the other state of functional React components, which generally trigger rerenders when updated. If you're ever wondered how to replace this.something = {} from React class components, useRef may be your solution.

Doesn't that useRef seem hacky? I thought so too, but I discovered that I wasn't the only one who used useRef this way. Dan Abramov, one of the most famous contributors to React and author of Redux / create-react-app, also uses useRef this way. Check out his blog post "Making setInterval Declarative with React Hooks" for more.

Note: you can get the code as a gist here.