Skip to content

Instantly share code, notes, and snippets.

@jakejrichards
Last active August 3, 2020 06:05
Show Gist options
  • Save jakejrichards/c1ce2f8b899854efdf0078f526e6a50d to your computer and use it in GitHub Desktop.
Save jakejrichards/c1ce2f8b899854efdf0078f526e6a50d to your computer and use it in GitHub Desktop.
import _ from "lodash"
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 PeopleComponent = ({ hairColor }: Record<"hairColor", string>) => {
const dispatch = useDispatch();
const people = useSelector(state => state.people);
useCollection<Person>(
() => firestore.collection("people").where("hairColor", "==", hairColor),
{
subscribe: () => dispatch({ type: "people/subscribe" }),
unsubscribe: () => dispatch({ type: "people/unsubscribe" }),
data: person => dispatch({ type: "people/data", person }),
error: error => dispatch({ type: "people/error", error })
},
[hairColor] // Ensure that we unsubscribe and re-subscribe when the hairColor changes
);
return (
<div>
<h1>People with {hairColor} hair.</h1>
<ul>
{_.map(people, person => (
<li key={person.id}>
{person.name}
</li>
))}
</ul>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment