A lightweight toolset for React developers.
中文 | English
- Memo optimization - Automatic event handler memoization
- Async data fetching - Simple hooks for API calls
- Zero dependencies - Tiny bundle size
- TypeScript first - Full type safety
npm i react-toolroomimport { memo } from 'react-toolroom';
// Automatically memoizes event handlers
const Button = memo(({ onClick, children }) => {
return <button onClick={onClick}>{children}</button>;
});import {
useResult,
useLoading,
useRun,
useInjectable,
useError,
useCache
} from 'react-toolroom/async';
// Create a data fetcher
const fetchUsers = useInjectable(async () => {
const res = await fetch('/api/users');
return res.json();
});
// Use in component
function UserList() {
const users = useResult(fetchUsers);
const loading = useLoading(fetchUsers);
const error = useError(fetchUsers);
useRun(fetchUsers, []);
if (loading) return <Spinner />;
if (error) return <Error error={error} />;
return <ul>{users.map(u => <li>{u.name}</li>)}</ul>;
}
// With caching
function CachedUserList() {
const stale = useCache(fetchUsers, cacheProvider, 60000);
// ...
}An enhanced version of React.memo that automatically memoizes event handlers.
memo(Component, { testEvent, propsAreEqual })| Option | Type | Description |
|---|---|---|
| testEvent | (key: string) => boolean | Test if prop is event handler (default: /^on[A-Z]/) |
| propsAreEqual | (prev, next) => boolean | Custom comparison |
Wraps an async function for use with other hooks.
Gets the result of an async function.
Gets the loading state of an async function.
Gets the error of a failed async function.
Runs a function when dependencies change.
Caches async function results with stale-while-revalidate.
Catches errors from async function.
Adds a handler that runs after async function completes.
Check demos for a complete example.
- painless - Frontend template
- native-router - Routing
Contributions are always welcome!
MIT