Skip to content

Instantly share code, notes, and snippets.

@designdevy
Forked from jakejrichards/Person.tsx
Created August 3, 2020 06:05
Show Gist options
  • Save designdevy/5117a79c2fae64d749c30b6bc2fa8f7d to your computer and use it in GitHub Desktop.
Save designdevy/5117a79c2fae64d749c30b6bc2fa8f7d to your computer and use it in GitHub Desktop.
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { Firestore } from "@google-cloud/firestore";
const firestore = new Firestore();
interface Person extends Entity {
name: string;
hairColor: string;
}
export const PersonComponent = ({ personId }: Record<"personId", string>) => {
const name = useSelector(state => state.people[personId].name);
const hairColor = useSelector(state => state.people[personId].hairColor);
const dispatch = useDispatch();
useDoc<Person>(
() => firestore.doc(personId),
{
subscribe: () => dispatch({ type: "person/subscribe", personId }),
unsubscribe: () => dispatch({ type: "person/unsubscribe", personId }),
data: person => dispatch({ type: "person/data", person }),
error: error => dispatch({ type: "person/error", error })
},
[personId] // Ensure that we unsubscribe and re-subscribe when the personId changes
);
return (
<div>
<h1>Name: {name}</h1>
<h3>Hair Color: {hairColor}</h3>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment