npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactopod

v0.5.0-beta.3

Published

React Hooks for Typesaurus, type-safe Firestore ODM

Downloads

4

Readme

🦕 Reactopod

React Hooks for Typesaurus, type-safe Firestore ODM.

Installation

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 preactopod

Note 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.

Get started

Initialization

To start working with Reactopod, initialize Firebase normally:

import * as firebase from 'firebase/app'
import 'firebase/firestore'

firebase.initializeApp({
  // Project configuration
})

See Firebase docs for more info.

Getting a single document

Use useGet hook to fetch document with the given id.

import React from 'react'
import { useGet } from 'reactopod'
import { collection } from 'typesaurus'

type User = { name: string }
const users = collection<User>('users')

function Component({ userId }: { userId: string }) {
  const user = useGet(users, userId)
  return user ? <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 React from 'react'
import { useOnGet } from 'reactopod'
import { collection } from 'typesaurus'

type User = { name: string }
const users = collection<User>('users')

function Component({ userId }: { userId: string }) {
  const user = useGet(users, userId)
  return user ? <div>Hello, {user.data.name}</div> : <div>Loading...</div>
}

Getting all documents from a collection

Use useAll hook to fetch all documents from a collection.

import React from 'react'
import { useAll } from 'reactopod'
import { collection } from 'typesaurus'

type User = { name: string }
const users = collection<User>('users')

function Component() {
  const allUsers = useAll(users)
  return allUsers ? (
    <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 React from 'react'
import { useOnAll } from 'reactopod'
import { collection } from 'typesaurus'

type User = { name: string }
const users = collection<User>('users')

function Component() {
  const allUsers = useOnAll(users)
  return allUsers ? (
    <ul>
      {allUsers.map(user => (
        <li>{user.data.name}</li>
      ))}
    </ul>
  ) : (
    <div>Loading...</div>
  )
}

Querying documents from a collection

Use useQuery hook to query documents from a collection using using query objects.

import React from 'react'
import { useQuery } from 'reactopod'
import { collection, where } from 'typesaurus'

type Game = { title: string; platform: 'switch' | 'xbox' | 'ps' | 'pc' }
const games = collection<Game>('games')

function Component() {
  const switchGames = useQuery(games, [where('platform', '==', 'switch')])
  return switchGames ? (
    <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 React from 'react'
import { useOnQuery } from 'reactopod'
import { collection, where } from 'typesaurus'

type Game = { title: string; platform: 'switch' | 'xbox' | 'ps' | 'pc' }
const games = collection<Game>('games')

function Component() {
  const switchGames = useOnQuery(games, [where('platform', '==', 'switch')])
  return switchGames ? (
    <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.

Querying documents with pagination

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 React from 'react'
import { useInfiniteQuery } from 'reactopod'
import { collection, where } from 'typesaurus'

type Game = {
  title: string
  platform: 'switch' | 'xbox' | 'ps' | 'pc'
  publishedAt: Date
}
const games = collection<Game>('games')

function Component() {
  const [switchGames, loadMore] = useInfiniteQuery(
    games,
    [where('platform', '==', 'switch')],
    { field: 'publishedAt', method: 'desc', limit: 10 }
  )

  return switchGames ? (
    <div>
      <ul>
        {switchGames.map(game => (
          <li>{game.data.title}</li>
        ))}
      </ul>

      {loadMore === undefined ? (
        <div>Loading more...</div>
      ) : loadMore === null ? null : (
        <button onClick={loadMore}>Load more</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 React from 'react'
import { useInfiniteQuery, useInfiniteScroll } from 'reactopod'
import { collection, where } from 'typesaurus'

type Game = {
  title: string
  platform: 'switch' | 'xbox' | 'ps' | 'pc'
  publishedAt: Date
}
const games = collection<Game>('games')

function Component() {
  const [switchGames, loadMore] = useInfiniteQuery(
    games,
    [where('platform', '==', 'switch')],
    { field: 'publishedAt', method: 'desc', limit: 10 }
  )
  useInfiniteScroll(1.5, loadMore)
  return switchGames ? (
    <ul>
      {switchGames.map(game => (
        <li>{game.data.title}</li>
      ))}
    </ul>
  ) : (
    <div>Loading...</div>
  )
}

Changelog

See the changelog.

License

MIT © Sasha Koss