@vilem/rcr
v1.2.5
Published
React call rest - typesafe react hooks for simple calling rest api.
Downloads
8
Maintainers
Readme
RCR
React call rest
This library, which contains a set of React hooks, was created to simplify the API calls from React components and to enhance the developer experience through its universality, simplicity, and built-in TypeScript support.
The library is suitable for applications with dynamic data rather than those with static data. In the future, WebSocket support will definitely be added.
Feel free to open a pull request, issue or leave a feedback.
Installation
npm
npm i @vilem/rcr
yarn
yarn add @vilem/rcr
Content
Hooks:
useFetchApi
- fetch data on component mountuseLazyFetchApi
- fetch data after calling callbackuseMutateApi
- send tata to server
Components:
RCRProvider
- component for wrapping applicatio with default RCR config
Usage
useFetchApi
The useFetchApi hook simplifies calling asynchronous APIs in React and automatically revalidates data upon argument changes. It's ideal for fetching data for display in an application and executes the fetcher function immediately upon mounting. It improves performance and is valuable for building dynamic user interfaces.
Example
You need to define a fetcher function, the fetch api, axios or whatever you want can be used in the fetcher function.
// userApi.ts
export interface User {
name: string
}
export const getUser = async (userId: number): Promise<User> => {
return (await axios.get(`/users/${userId}`)).data;
}
Using hook in component:
// User.tsx
import { getUser } from './userApi.ts';
import { useFetchApi } from '@vilem/rcr';
export default User = () => {
const request = useFetchApi(getUser, 1);
// a configuration object can also be used
const requestWithOptions = useFetchApi(getUser, {
revalidateOnArgsChange: false,
revalidateOnFocus: false
}, 1)
if (request.loading || request.revalidating) return (
<div>
Loading...
</div>
)
return (
<div>
<p>User name: {request.data.name}</p>
</div>
)
}
The request object:
call
- Function that reloads all dataloading
- Boolean that indicates the first loading of datadata
- This is the returned value of fetcher functionerrorMessage
- Error message which is thrown by fetcher functionrevalidate
- Function which performs like call but loading is false while revalidatingrevalidating
- Boolean that indicates revalidation
useMutateApi
The useMutateApi hook is suitable for sending data to server.
Example
Again we need a fetcher function, but now it register nwe user.
// userAPi.ts
// ...
export const registerUser = async (userName: string): Promise<void> => {
await axios.post('/users', { userName });
}
Using hook in component:
// RegisterUser.tsx
import { registerUser } from './userApi.ts';
import { useMutateApi } from '@vilem/rcr';
import { useState } from 'react';
export default User = () => {
const [name, setName] = useState<string | null>(null);
const request = useMutateApi(registerUser);
const handleSubmit = async () => {
const {data, errorMessage} = await request.call(name);
console.log(data);
console.log(errorMessage);
}
return (
<div>
<span>User name</span>
<input type="text" onChange={(e) => {setName(e.target.value)}} value={name} />
<button
onClick={handleSubmit}
>
{request.loading ? 'Sending...' : 'Send'}
</button>
</div>
)
}
The request object:
call
- Function that reloads all dataloading
- Boolean that indicates the first loading of datadata
- This is the returned value of fetcher functionerrorMessage
- Error message which is thrown by fetcher function
Note: Currently the useMutateApi hook works the same as useLazyFetchApi.