@helpful-hooks/use-fetch
v2.0.0
Published
Helpful React hook for api calls
Downloads
2
Readme
Use Fetch
Yet another hook in your @helpful-hooks
toolbox!
The @helpful-hooks/use-fetch
package is designed to be a lightweight, flexible and extensible solution for making fetch requests and api calls
It follows a similar design to rtk-query, but allows for easy and complete customization over the raw requests and responses, when requests are sent, whether they are sent at all, how urls, request and response payloads are processed before being sent on their way and much more.
Excited by the possibilities? Read on to find out more!
Contents
Installation
With yarn
yarn add @helpful-hooks/use-fetch
With npm
npm install @helpful-hooks/use-fetch
Basic Usage
The useFetch
hook is filled with many useful features to help with api calls. Below is the basic gist of how useFetch
is meant to be used in a React app which displays a list of Todos from an api.
However, as there are too many uses and techniques to cleany demonstrate in a single README, please see the In-Depth Usage section for links to further documentation
api.ts
import { useFetch } from '@helpful-hooks/use-fetch'
// Database Types
interface Todo {
id: number
name: string
completed: boolean
}
interface SearchTodosParams {
id?: number
name?: string
completed?: boolean
}
interface CreateTodoBody {
name: string
}
// useFetch can be used directly in a component, but is far cleaner and DRYer when abstracted over in a custom hook
export const useSearchTodosQuery = (params: SearchTodosParams) =>
useFetch<Todo[], SearchTodosParams>({
queryArgs: params,
query: (args) => ({
url: 'https://my-api.com/todos',
params: args
})
})
export const useCreateTodoQuery = () =>
useFetch<Todo, CreateTodoBody>({
triggerOnLoad: false,
query: (args) => ({
url: 'https://my-api.com/todos',
method: 'POST',
body: args
})
})
Todos.tsx
import { useSearchTodosQuery, useCreateTodoQuery } from './api'
export const Todos = () => {
const [searchTodosParams] = useState({ completed: true })
const searchTodosQuery = useSearchTodosQuery(searchTodosParams)
const createTodoQuery = useCreateTodoQuery()
const createTodo = async (name: string) => {
// Creates a new Todo with specified name,
// then re-runs the search query to update the list of Todo's
await createTodoQuery.trigger({ name })
await searchTodosQuery.trigger(searchTodosParams)
}
return (
<>
<h1>Things left to do:</h1>
{searchTodosQuery.loading && <p>Loading...</p>}
{searchTodosQuery.error && <p>Could not retreive Todos</p>}
{searchTodosQuery.data && searchTodosQuery.data.map((todo, i) => (
<p>{i} - {todo.name}</p>
))}
<hr />
<h1>Add something new to do:</h1>
<button
disabled={createTodoQuery.loading}
onClick={() => {
const todoName = getNewTodoName()
createTodo(todoName)
}}>
Generate new Todo
</button>
</>
)
}
In-Depth Usage
If you would like to find out about the other cool fetures provided by this hook, check out the links below to our documentation pages:
- Common patterns
- Customizing requests
- Manually triggering requests
- Modifying responses
- Custom fetchers
- Typescript tips
Licence
This package uses the MIT licence. Feel free to use it in whatever morally correct way you'd like
Contributing
All contributions are welcome. If you notice any bugs or have any feature request or questions, please open an issue in our Github repo