1.1.0 • Published 2 years ago

react-query-useful-hooks v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-query-useful-hooks

The best and useful hooks for react-query

GitHub branch checks state GitHub issues GitHub npm Website GitHub language count GitHub top language GitHub code size in bytes

Features

  • All the axios awesomeness you are familiar with
  • Zero configuration, but configurable if needed
  • One-line usage

Installation

npm install axios react-query react-query-useful-hooks

axios and react-query is a peer dependency and needs to be installed explicitly

Quick Start

import React from 'react';
import {useFetch} from 'react-query-useful-hooks';

import React from 'react';
import {useFetch} from 'react-query-useful-hooks';

function Todos() {
    const {isError, isFetching, data, refetch} = useFetch({
        url: 'todos/1',
        enabled: true
    });

    if (isFetching) return <p>Loading...</p>;
    if (isError) return <p>Error!</p>;
    return (
        <div>
            <button onClick={() => refetch()}>refetch</button>
            <pre>{JSON.stringify(data, null, 2)}</pre>
        </div>
    );
}

export default Todos;

API

The package exports one default export and named exports:

import {
    useFetch,
    useInfinite,
    usePaginate,
    usePost,
    useModifyQuery,
    usePersist,
    useSubscribeQuery,
    configure
} from 'react-query-useful-hooks';

Configuration

Unless provided via the configure function, react-query-useful-hooks uses as defaults:

These defaults may not suit your needs, for example:

  • you may want a common base url or timeout for axios requests
  • need to customize cacheTime or staleTime
  • or different behavior in onSuccess and onError requests

In such cases you can use the configure function to provide your custom implementation of both.

When configure is used, it should be invoked once before any usages of hooks

Example

import { configure } from 'react-query-useful-hooks'
import axios from 'axios'

configure({
    axios: axios.create({
        baseURL: 'https://jsonplaceholder.typicode.com',
        timeout: 1000
    }),
    queryOptions: {
        retry: 5,
        retryDelay: 100,
        retryOnMount: false
    }
});

Creator

Sina Shah Oveisi @sinashahoveisi

I love programming, and I am interested in popular frameworks or programming languages and I am currently coding with JavaScript and React framework.


License

MIT © Sina Shahoveisi

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago