@rest-hooks/react
v8.2.2
Published
Normalized state management for async data. Safe. Fast. Reusable.
Downloads
3,552
Readme
Define your async methods. Use them synchronously in React. Instantly mutate the data and automatically update all usages.
For REST, GraphQL, Websockets+SSE and more
📖Read The Docs | 🏁Getting Started | 🎮Todo Demo | 🎮Github Demo | 🎮NextJS SSR Demo
Installation
npm install @rest-hooks/react @rest-hooks/rest @rest-hooks/test
For more details, see the Installation docs page.
Simple TypeScript definition
class Article extends Entity {
id = '';
title = '';
body = '';
pk() {
return this.id;
}
}
Create collection of API Endpoints
const ArticleResource = createResource({
path: '/articles/:id',
schema: Article,
})
One line data binding
const article = useSuspense(ArticleResource.get, { id });
return (
<>
<h2>{article.title}</h2>
<p>{article.body}</p>
</>
);
Mutation
const ctrl = useController();
return (
<ArticleForm
onSubmit={data => ctrl.fetch(ArticleResource.update, { id }, data)}
/>
);
Subscriptions
const price = useLive(PriceResource.get, { symbol });
return price.value;
Programmatic queries
const sortedArticles = new Query(
new schema.All(Article),
(entries, { asc } = { asc: false }) => {
const sorted = [...entries].sort((a, b) => a.title.localeCompare(b.title));
if (asc) return sorted;
return sorted.reverse();
}
);
const articlesUnsorted = useCache(sortedArticles);
const articlesAscending = useCache(sortedArticles, { asc: true });
const articlesDescending = useCache(sortedArticles, { asc: false });
...all typed ...fast ...and consistent
For the small price of 9kb gziped. 🏁Get started now | 🥊Comparison
Features
- [x] Strong Typescript inference
- [x] 🛌 React Suspense support
- [x] 🧵 React 18 Concurrent mode compatible
- [x] 💦 Partial Hydration Server Side Rendering
- [x] 🎣 Declarative API
- [x] 📝 Composition over configuration
- [x] 💰 Normalized caching
- [x] 💥 Tiny bundle footprint
- [x] 🛑 Automatic overfetching elimination
- [x] ✨ Optimistic updates
- [x] 🧘 Flexible to fit any API design (one size fits all)
- [x] 🔧 Debugging and inspection via browser extension
- [x] 🌳 Tree-shakable (only use what you need)
- [x] 🔁 Subscriptions
- [x] ♻️ Optional redux integration
- [x] 📙 Storybook mocking
- [x] 📱 React Native support
- [x] ⚛️ NextJS support
- [x] 🚯 Declarative cache lifetime policy
- [x] 🧅 Composable middlewares
- [x] 💽 Global data consistency guarantees
- [x] 🏇 Automatic race condition elimination
- [x] 👯 Global referential equality guarantees