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

@featherweight/react-resource-ts

v0.5.0

Published

React bindings for Resource ADT

Downloads

10

Readme

@featherweight/react-resource-ts

Intro

React Resource is a React Hooks library for data fetching based on Resource ADT

Install

npm install --save @featherweight/react-resource-ts @featherweight/resource-ts fp-ts

Don't forget to install peer dependencies: @featherweight/resource-ts and fp-ts.

Quick start

First, you need to wrap you app with ResourceProvider.

import React from 'react'
import {useResource, Succeded, Failed} from '@featherweight/react-resource-ts'

import {fetchArticles} from './api'

const Articles = () => {
  const articles = useResource(fetchArticles)

  if (articles.isInitial) return null

  if (articles.isPending) return <div>Loading articles...</div>

  return (
    <div>
      <Succeded of={articles.resource}>
        {articles => (
          <ul>
            {articles.map(article => (
              <li>{article.title}</li>
            ))}
          </ul>
        )}
      </Succeded>
      <Failed of={articles.resource}>
        <div>Cannot load articles =(</div>
      </Failed>
    </div>
  )
}

API

useResource

useResource<O, D, E>(load, config?), where O - options for load fuction; D - response type; E - error type;

const {
  cancel: () => void,
  error?: E,
  isFailed: boolean,
  isInitial: boolean,
  isPending: boolean,
  isSucceded: boolean,
  reset: () => void,
  resource: Resource<D, E>,
  run: (o: O) => Promise<Resource<D, E>>,
  state: string,
  value?: D,
} = useResource<O, D, E>(
  loadFn: (o: O, meta: Meta) => Promise<D>,
  config?: {
    args?: O
    chain?: Resource<any, any>
    defer?: boolean
    dependencies?: any[]
    namespace?: string
    onFail?: (e: E) => void
    onSuccess?: (v: D) => void
    reducer?: CustomReducer<D, E>
    skipPending?: number
  }
)

useResource.withError

useResource.withError<E>() binds error type and returns useResource<_, _, E>.

import {HTTPErrorType} from './http'

const useHttpResource = useResource.withError<HTTPErrorType>()

useTask

useTask<O, D> is an alias for useResource<O, D, Error>.

create

Factory function for creating pre-configured useResource hook.

const useUserResource = create(fetchUser, {namespace: 'user'})

create.withError

Binds error to create factory function.

const useUserResource = create.withError<HttpErrorType>()(fetchUser, {
  namespace: 'user',
})

Components

You can use helper components to declaratively render ui based on resource state.

import {
  Initial,
  Pending,
  Succeded,
  Failed,
} from '@featherweight/react-resource-ts'

const user = useResource(fetchUser)

const rendered = (
  <>
    <Succeded of={user.resource}>
      {(user) => <div>{user.name}</div>}
    </Succeded>
    <Failed of={user.resource}>
      {(error) => <div>{error.toString()}</div>}
    </Failed>
    <Pending of={user.resource} render={() => <div>Loading...</div>}>
    <Initial of={user.resource}>
      <div>Nothing there yet</div>
    </Initial>
  </>
)