MCPcopy Index your code
hub / github.com/FirebaseExtended/reactfire / FavoriteAnimals

Function FavoriteAnimals

example/withSuspense/Firestore.tsx:82–117  ·  view source on GitHub ↗
(props)

Source from the content-addressed store, hash-verified

80};
81
82const FavoriteAnimals = (props) => {
83 const firestore = useFirestore();
84 const animalsCollection = collection(firestore, 'animals');
85 const [isAscending, setIsAscending] = useState(true);
86 const animalsQuery = query(animalsCollection, orderBy('commonName', isAscending ? 'asc' : 'desc'));
87 const [isPending, startTransition] = useTransition();
88
89 const toggleSort = () => {
90 startTransition(() => {
91 setIsAscending(!isAscending);
92 });
93 };
94
95 const addAnimal = () => {
96 const possibleAnimals = ['Dog', 'Cat', 'Iguana', 'Zebra'];
97 const selectedAnimal = possibleAnimals[Math.floor(Math.random() * possibleAnimals.length)];
98 addDoc(animalsCollection, { commonName: selectedAnimal });
99 };
100
101 const removeAnimal = (id) => deleteDoc(doc(animalsCollection, id));
102
103 return (
104 <>
105 <WideButton label="Sort" onClick={toggleSort} />
106 <React.Suspense fallback="loading...">
107 <List query={animalsQuery} removeAnimal={removeAnimal} />
108 </React.Suspense>
109 <WideButton
110 label="Add Animal"
111 onClick={() => {
112 addAnimal();
113 }}
114 />
115 </>
116 );
117};
118
119function FirestoreWrapper({ children }) {
120 const { data: firestoreInstance } = useInitFirestore(async (firebaseApp) => {

Callers

nothing calls this directly

Calls 2

useFirestoreFunction · 0.85
addAnimalFunction · 0.70

Tested by

no test coverage detected