npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@helpful-hooks/use-fetch

v2.0.0

Published

Helpful React hook for api calls

Downloads

4

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:

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