rsrc

1.3.0 • 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((value) => {
              /* */
            })
            .catch((error) => {
              /* */
            });
        };

        return (
          <div>
            <h1>{todo.name}</h1>
            <p>{todo.description}</p>
            <button onClick={handleClick}>×</button>
          </div>
        );
      }}
    </Resource>
  );
};

Related

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.3.0
    9
    • latest
  • 1.0.0
    0
    • 1.0.0

Version History

Package Sidebar

Install

npm i rsrc

Weekly Downloads

24

Version

1.3.0

License

MIT

Unpacked Size

55.6 kB

Total Files

8

Last publish

Collaborators

  • johnmdonahue
  • leaf-sigsci
  • sigsci
  • alitorbati