React Hooks for Typesaurus, type-safe Firestore ODM.
The library is available as an npm package. To install Reactopod run:
# React:
npm install reactopod --save
# Or using Yarn:
yarn add reactopod
# Preact:
npm install preactopod --save
# Or using Yarn:
yarn add preactopodNote that Reactopod has Typesaurus listed as a peer dependency which also requires firebase package to work in the web environment. The latter isn't listed in dependencies, so make sure you did install both. For more info about Typesaurus dependencies, refer to its Installation section of README. Also, if you have to have react or preact installed for reactopod and preactopod respectively.
To start working with Reactopod, initialize Firebase normally:
import*asfirebasefrom'firebase/app'import'firebase/firestore'firebase.initializeApp({// Project configuration})See Firebase docs for more info.
Use useGet hook to fetch document with the given id.
import{createElement}from'react'import{useGet}from'reactopod'import{collection}from'typesaurus'typeUser={name: string}constusers=collection<User>('users')functionComponent({ userId }: {userId: string}){constuser=useGet(users,userId)returnuser ? <div>Hello,{user.data.name}</div>:<div>Loading...</div>}Use useOnGet hook to subscribe to a document with the given id. When the document changes you'll receive the new data automatically.
import{createElement}from'react'import{useOnGet}from'reactopod'import{collection}from'typesaurus'typeUser={name: string}constusers=collection<User>('users')functionComponent({ userId }: {userId: string}){constuser=useGet(users,userId)returnuser ? <div>Hello,{user.data.name}</div>:<div>Loading...</div>}Use useAll hook to fetch all documents from a collection.
import{createElement}from'react'import{useAll}from'reactopod'import{collection}from'typesaurus'typeUser={name: string}constusers=collection<User>('users')functionComponent(){constallUsers=useAll(users)returnallUsers ? (<ul>{allUsers.map(user=>(<li>{user.data.name}</li>))}</ul>) : (<div>Loading...</div>)}Use useOnAll hook to subscribe to all documents within a collection. When a document in the collection changes you'll receive the new data.
import{createElement}from'react'import{useOnAll}from'reactopod'import{collection}from'typesaurus'typeUser={name: string}constusers=collection<User>('users')functionComponent(){constallUsers=useOnAll(users)returnallUsers ? (<ul>{allUsers.map(user=>(<li>{user.data.name}</li>))}</ul>) : (<div>Loading...</div>)}Use useQuery hook to query documents from a collection using using query objects.
import{createElement}from'react'import{useQuery}from'reactopod'import{collection,where}from'typesaurus'typeGame={title: string;platform: 'switch'|'xbox'|'ps'|'pc'}constgames=collection<Game>('games')functionComponent(){constswitchGames=useQuery(games,[where('platform','==','switch')])returnswitchGames ? (<ul>{switchGames.map(game=>(<li>{game.data.title}</li>))}</ul>) : (<div>Loading...</div>)}Use useOnQuery hook to subscribe to a query results. When the result changes you'll receive the new data.
import{createElement}from'react'import{useOnQuery}from'reactopod'import{collection,where}from'typesaurus'typeGame={title: string;platform: 'switch'|'xbox'|'ps'|'pc'}constgames=collection<Game>('games')functionComponent(){constswitchGames=useOnQuery(games,[where('platform','==','switch')])returnswitchGames ? (<ul>{switchGames.map(game=>(<li>{game.data.title}</li>))}</ul>) : (<div>Loading...</div>)}See Typesaurus documentation for more info about the query objects:
order- Creates order query object with given field, ordering method and pagination cursors.limit- Creates a limit query object. It's used to paginate queries.where- Creates where query with array-contains filter operation.
Pagination helpers:
endAt- Ends the query results on the given value.endBefore- Ends the query results before the given value.startAfter- Start the query results after the given value.startAt- Start the query results on the given value.
Use useInfiniteQuery to query documents with pagination.
The function returns an array where the first element is the result and the second is loadMore. loadMore is undefined when the result is loading. loadMore is null when there're no more data to load. Otherwise loadMore is a function that triggers loading of the next page.
import{createElement}from'react'import{useInfiniteQuery}from'reactopod'import{collection,where}from'typesaurus'typeGame={title: stringplatform: 'switch'|'xbox'|'ps'|'pc'publishedAt: Date}constgames=collection<Game>('games')functionComponent(){const[switchGames,loadMore]=useInfiniteQuery(games,[where('platform','==','switch')],{field: 'publishedAt',method: 'desc',limit: 10})returnswitchGames ? (<div><ul>{switchGames.map(game=>(<li>{game.data.title}</li>))}</ul>{loadMore===undefined ? (<div>Loadingmore...</div>) : loadMore===null ? null : (<buttononClick={loadMore}>Loadmore</button>)}</div>) : (<div>Loading...</div>)}Combine useInfiniteQuery with useInfiniteScroll to implement the infinite scroll.
useInfiniteScroll accepts two arguments. The first is the scroll threshold. If you pass 1.5, then more results will load when the scroll position is more than full height minus 1.5 x screen height. The second argument is loadMore function returned from useInfiniteQuery.
import{createElement}from'react'import{useInfiniteQuery,useInfiniteScroll}from'reactopod'import{collection,where}from'typesaurus'typeGame={title: stringplatform: 'switch'|'xbox'|'ps'|'pc'publishedAt: Date}constgames=collection<Game>('games')functionComponent(){const[switchGames,loadMore]=useInfiniteQuery(games,[where('platform','==','switch')],{field: 'publishedAt',method: 'desc',limit: 10})useInfiniteScroll(1.5,loadMore)returnswitchGames ? (<ul>{switchGames.map(game=>(<li>{game.data.title}</li>))}</ul>) : (<div>Loading...</div>)}See the changelog.