Fetching hooks for React
npm install @evergis/react-fetchingFetching hooks for React
Create resource
``javascript
export const people = createResource(
// thunk
() => async () => {
const response = await fetch('https://swapi.co/api/people');
const { results, ...meta } = await response.json();
return {
data: results, // entity or entity array
meta, // response meta data
};
}
);
`
You can inject your api in thunk
`javascript`
export const people = createResource(
(query?: string) => async swapi => {
const { results, ...meta } = await swapi.getPeople(query);
return {
data: results,
meta,
};
},
{ injected: swapi } // inject api in thunk
);
For debug you need enable debug mode and pass resource name
`javascript`
export const people = createResource(
//...,
{ name: 'people', debug: true }
);
Resource have use hook. This hook used to fetch data
`javascript``
export const People: React.FC = () => {
const { data, error, loading } = people.use({ input: '' });
return
};
// TODO mutation and other functionality