0.0.7 • Published 2 years ago

@valu/create-async-hook v0.0.7

Weekly downloads
6
License
ISC
Repository
-
Last release
2 years ago

React Hook Factory for Async Functions

With focus on simple pagination.

Install

npm install @valu/create-async-hook

Usage

Example with babel-gql

import { createAsyncHook } from "@valu/create-async-hook";
import { gql, request } from "babel-gql";

const getPostsQuery = gql`
    query getPosts($cursor: String) {
        posts(after: $cursor) {
            pageInfo {
                hasNextPage
                endCursor
            }
            nodes {
                id
                title
            }
        }
    }
`;

const usePagedPosts = createAsyncHook(
    async function(variables) {
        return request("/graphql", {
            query: getPostsQuery,
            variables: {
                cursor: variables.cursor,
            },
        });
    },
    {
        initialState: {
            endCursor: null,
            hasNextPage: true,
            posts: [],
        },
        update(state, response, meta) {
            return {
                posts: [...state.posts, ...res.data.posts.nodes],
                endCursor: res.data.posts.pageInfo.endCursor,
                hasNextPage: res.data.posts.pageInfo.hasNextPage,
            };
        },
    },
);

function Component() {
    const [cursor, setCursor] = React.useState(null);

    const res = usePagedPosts({ variables: { cursor: cursor } });

    const fetchMore = () => {
        setCursor(res.state.endCursor);
    };

    if (res.error) {
        return <div>error</div>;
    }

    return (
        <>
            <ul>
                {res.state.posts.map(post => (
                    <li key={post.id}>{post.title}</li>
                ))}
            </ul>
            <button onClick={res.fetchMore}>more</button>
            <Spinner show={res.loading} />
        </>
    );
}
0.0.7

2 years ago

0.0.5

3 years ago

0.0.6

3 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago