Crema React
v-4 CRA
Search
K
Comment on page

APIs Calling

Crema has some predefined hooks and functions to call the APIs without writing redundant code like sending tokens and other extra configurations in APIs.
Crema has some predefined hooks and functions to call the APIs without writing redundant code like sending tokens and other extra configurations in APIs. These functions are directly linked with your authentication method as well, so you don't need anything special to call APIs without any extra setup.
  1. 1.
    GET: to fetch the data from the API. We can use this pre-defined hook
const [
{apiData, loading, initialUrl},
{setData, setLoading, updateInitialUrl, setQueryParams, reCallAPI},
] = useGetDataApi('/dashboard/academy', {data: []}, {page: 1, perPage: 10});
  1. 2.
    POST: To save data using the API function
const infoViewActionsContext = useInfoViewActionsContext();
postDataApi('/wall/posts', infoViewActionsContext, {
post,
})
.then((data) => {
// Do anything that you want here
infoViewActionsContext.showMessage('Post Created Successfully!');
})
.catch((error) => {
infoViewActionsContext.fetchError(error.message);
});
  1. 3.
    PUT: To save data using the API function
const infoViewActionsContext = useInfoViewActionsContext();
putDataApi('/wall/posts/${post.id}', infoViewActionsContext, {
post,
})
.then((data) => {
// Do anything that you want here
infoViewActionsContext.showMessage('Post Updated Successfully!');
})
.catch((error) => {
infoViewActionsContext.fetchError(error.message);
});
  1. 4.
    DELETE: To Delete entry using the API function
const infoViewActionsContext = useInfoViewActionsContext();
deleteDataApi('/wall/posts/${post.id}', infoViewActionsContext)
.then((data) => {
// Do anything that you want here
infoViewActionsContext.showMessage('Post Deleted Successfully!');
})
.catch((error) => {
infoViewActionsContext.fetchError(error.message);
});