Publish your own packages to the world's most popular software ecosystem with npm Pro.Get started »

rsrc

1.1.1 • Public • Published

Logo

Rsrc

ci version license gzip size

A collection of components designed to simplify fetch state in React.

Docs

Getting Started

yarn add rsrc

Usage

import React from "react";
import { Resource } from "rsrc";
 
export default props => {
  const { id } = props;
  const url = `/todos/${id}`;
 
  return (
    <Resource
      url={url}
      maxAge={60}
      actions={{
        remove: () => ({
          options: {
            method: "DELETE"
          },
          invalidates: ["/todos"]
        })
      }}
    >
      {({ state, actions }) => {
        if (!state.fulfilled) return null;
 
        const todo = state.value;
 
        const handleClick = () => {
          actions
            .remove()
            .then(fetchState => {
              /* */
            })
            .catch(error => {
              /* */
            });
        };
 
        return (
          <div>
            <h1>{todo.name}</h1>
            <p>{todo.description}</p>
            <button onClick={handleClick}>×</button>
          </div>
        );
      }}
    </Resource>
  );
};

Related

Install

npm i rsrc

DownloadsWeekly Downloads

17

Version

1.1.1

License

MIT

Unpacked Size

49.5 kB

Total Files

8

Last publish

Collaborators

  • avatar
  • avatar