A collection of components designed to simplify fetch state in React.
Effective May 17th 2021 the default branch will change from master
to main
. Run the following commands to update a local clone:
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
yarn add rsrc
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>
);
};