This package has been deprecated

Author message:

WARNING: This project has been renamed to react-create-api-hook. Install using react-create-api-hook instead.

react-hook-use-api-method
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

Usage

const [data, getData, status] = useApi(yourApiMethod);

yourApiMethod - a function that make request, should return Promise;

data - data you want to get;

getData - function that shoukd be invoked to start data fetching;

status.pending - pending indicator (boolean);

status.error - null if no errors

Example

import React from "react";
import axios from "axios";
import { useApi } from "react-hook-use-api-method";

interface Post {
    id: number;
    title: string;
}

function getPostsMethod(args: string) {
    // You can use fetch or any function that return promise
    return axios
        .request<Post[]>({
            // request config
        })
        .then((response) => {
            return response.data;
        });
}

function App() {
    const [posts, getPosts, status] = useApi(getPostsMethod);
    const loadPosts = React.useCallback(() => {
        // pass args for api method
        getPosts("any args if needed");
    }, [getPosts]);

    return (
        <div>
            <button disabled={status.pending} onClick={loadPosts}>
                {status.pending ? "Loading..." : "Load posts"}
            </button>
            <div>
                {status.error && status.error.toString()}
                {posts ? (
                    posts.map((post) => (
                        <div key={post.id}>
                            <h2>{post.title}</h2>
                        </div>
                    ))
                ) : (
                    <div>You have no posts</div>
                )}
            </div>
        </div>
    );
}

export default App;

Readme

Keywords

Package Sidebar

Install

npm i react-hook-use-api-method

Weekly Downloads

6

Version

0.0.5

License

MIT

Unpacked Size

7.49 kB

Total Files

9

Last publish

Collaborators

  • skazko