0.6.9 • Published 7 months ago

react-query-crud v0.6.9

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

workflow npm version

React query CRUD

This library is built on top of two libraries:

It's used for creating CRUD lists

Installation

# npm
npm install --save @tanstack/react-query react-query-crud
# yarn
yarn add @tanstack/react-query react-query-crud

Getting started

First you should wrap your app with QueryCrudClientProvider

import { QueryClient } from '@tanstack/react-query';
import { QueryCrudClientProvider } from 'react-query-crud';

const queryClient = new QueryClient();

const App = () => <QueryCrudClientProvider client={queryClient}>{/* your app */}</QueryCrudClientProvider>;

Let's say you have an API like this:

import methods from './methods';

type Item = {
    id: string;
    name: string;
};


export default {
    create: (item: { name: string }): Promise<Item> = methods.post('/items', {name}),
    delete: ({ id }: { id: number }): Promise<void> => methods.delete(`items/${id}`),
    list: (): Promise<Item[]> => methods.get('/items'),
    update: ({ id, name }: { id: number; name: string }): Promise<Item> => methods.put(`items/${id}`, { name }),
};

Then you need to create a hook that will incapsulate all the logic for specific CRUD list:

import api from './api';
import {
    useCrudListQuery,
    useCrudListUpdater,
    useNonNormalizedMutation,
    useNormalizedMutation,
} from 'react-query-crud';

export const useItems = () => {
    const key = ['items'];
    const typename = 'item';

    const onCreate = useCrudListUpdater<number, Item, { name: string }, Item>({
        key,
        update: (items, result) => [...items, result],
    });
    const onDelete = useCrudListUpdater<number, Item, { id: number }, void>({
        key,
        update: (items, _result, props) => items.filter((item) => item.id !== props.id),
    });

    const create = useNormalizedMutation({
        run: (props: { name: string }) => api.create(props),
        update: onCreate,
        typename,
    });
    const del = useNonNormalizedMutation({
        run: (props: { id: number }) => api.delete(props),
        update: onDelete,
    });
    const read = useCrudListQuery<number, Item>({ key, fetch: () => api.list(), typename });
    const update = useNormalizedMutation({
        run: (props: { id: number; name: string }) => api.update(props),
        typename,
    });

    return {
        create,
        delete: del,
        read,
        update,
    };
};

Then you can use it in your component:

import { useCallback } from 'react';
const Comp = () => {
    const itemsAPI = useItems();
    const items = itemsAPI.read();

    const onClickCreate = useCallback(() => itemsAPI.create({ name: 'New item' }), [items.create]);
    const onClickDelete = useCallback((id: string) => itemsAPI.delete({ id }), [items.delete]);
    const onClickUpdate = useCallback(
        (id: string, newName: string) => itemsAPI.update({ id, name: newName }),
        [items.update],
    );

    return (
        <div>
            {items.value.map((item) => (
                <div key={item.id}>
                    {item.name}
                    <button onClick={() => onClickUpdate(item.id, 'New name')}>Update</button>
                    <button onClick={() => onClickDelete(item.id)}>Delete</button>
                </div>
            ))}
            <button onClick={onClickCreate}>Create</button>
        </div>
    );
};

Documentation

Query

You have three types of CRUD entities:

  • useCrudQuery
  • useCrudInfiniteListQuery
  • useCrudListQuery

Each of them return an object with fields:

  • query - react-query object
  • value - flattened value

Query updater

For each type you have a corresponding updater that can be passed to mutations:

  • useCrudUpdater
  • useCrudListUpdater
  • useCrudInfiniteListUpdater

Mutation

There are also two types of mutations:

  • useNormalizedMutation
  • useNonNormalizedMutation

Normalized mutation should always return the type of the entity, while non-normalized mutation can return anything. The entity returned by normalized mutation will be used to update the normalization object (in react-query cache).

If you want to use normalization in objects inside nested arrays just add __typename field to each object.

0.6.7

7 months ago

0.6.6

7 months ago

0.6.9

7 months ago

0.6.3

7 months ago

0.6.2

7 months ago

0.6.5

7 months ago

0.6.4

7 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.1.10

10 months ago

0.1.11

10 months ago

0.4.9

10 months ago

0.1.12

10 months ago

0.4.8

10 months ago

0.4.10

10 months ago

0.4.13

10 months ago

0.4.12

10 months ago

0.3.0

10 months ago

0.1.2

10 months ago

0.2.0

10 months ago

0.1.1

10 months ago

0.5.4

8 months ago

0.5.3

9 months ago

0.4.4

10 months ago

0.1.7

10 months ago

0.4.7

10 months ago

0.1.9

10 months ago

0.5.0

9 months ago

0.4.1

10 months ago

0.3.2

10 months ago

0.1.4

10 months ago

0.4.0

10 months ago

0.3.1

10 months ago

0.1.3

10 months ago

0.5.2

9 months ago

0.4.3

10 months ago

0.1.6

10 months ago

0.4.2

10 months ago

0.1.5

10 months ago

0.1.0

10 months ago