useCustom

call useCustom manually
16 Replies
kapa.ai
kapa.ai8mo ago
Hi @parothia. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To call the useCustom hook manually in refine, you need to import it from @refinedev/core and then use it in your component. The useCustom hook expects the url and method properties, which will be passed to the custom method from the dataProvider as parameters. When properties are changed, the useCustom hook will trigger a new request. Here is a basic usage example:
import { useCustom, useApiUrl } from "@refinedev/core";

interface PostUniqueCheckResponse {
isAvailable: boolean;
}

const apiUrl = useApiUrl();

const { data, isLoading } = useCustom<PostUniqueCheckResponse>({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
});
import { useCustom, useApiUrl } from "@refinedev/core";

interface PostUniqueCheckResponse {
isAvailable: boolean;
}

const apiUrl = useApiUrl();

const { data, isLoading } = useCustom<PostUniqueCheckResponse>({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
});
In this example, useApiUrl is used to get the base URL of the API endpoint. The useCustom hook is then used to send a request to the endpoint. The url property accepts the endpoint URL, and the method property accepts the type of method of the endpoint. The config property accepts values like the headers for the endpoint, query parameters, and many more. For more information, you can refer to the useCustom documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCustom/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1149386202403844156> kapa.ai is still learning and improving, please let us know how it did by reacting below
adverse-sapphire
adverse-sapphire8mo ago
@kapa.ai const { isLoading, data, refetch, } = useCustom< BankStatementEntryListResponse, ApiErrorResponse, BankStatementEntrySearchRequest >({ url: ${apiUrl}/${apiPaths.bankStatement.entries}, method: 'get', queryOptions: { enabled: false, }, config: { query: { pageSize: searchRequest.pageSize, pageNumber: searchRequest.pageNumber, searchText: searchRequest.searchText, status: searchRequest.status, fromDate: searchRequest.fromDate, toDate: searchRequest.toDate, bankUuid: searchRequest.bankUuid, entryType: searchRequest.entryType, }, }, errorNotification: (error: ApiErrorResponse | undefined) => { return { message: t('pages.bankStatement.entriesList.errorNotificationMessage'), type: 'error', description: error?.detailedMessage, }; }, successNotification: false, }); call this api manually
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books:
rival-black
rival-black8mo ago
hello @parothia , refine uses react-query under the hood. I believe this can be helpfull https://stackoverflow.com/questions/62340697/react-query-how-to-usequery-when-button-is-clicked
kapa.ai
kapa.ai8mo ago
---------------------- To call the API manually using the useCustom hook in the refine framework, you can use the refetch function that is returned by the hook. The refetch function allows you to manually trigger the API call whenever you need it. In your case, you have disabled the initial API call by setting enabled: false in queryOptions. So, you can call the API manually by invoking the refetch function. Here is an example of how you can do it:
const {
isLoading,
data,
refetch,
} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({
url: `${apiUrl}/${apiPaths.bankStatement.entries}`,
method: 'get',
queryOptions: {
enabled: false,
},
config: {
query: {
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
},
},
errorNotification: (error: ApiErrorResponse | undefined) => {
return {
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
};
},
successNotification: false,
});

// Call the API manually
refetch();
const {
isLoading,
data,
refetch,
} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({
url: `${apiUrl}/${apiPaths.bankStatement.entries}`,
method: 'get',
queryOptions: {
enabled: false,
},
config: {
query: {
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
},
},
errorNotification: (error: ApiErrorResponse | undefined) => {
return {
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
};
},
successNotification: false,
});

// Call the API manually
refetch();
In this example, refetch() is used to manually trigger the API call. You can call this function wherever you need to refresh the data. For more information, you can refer to the useCustom documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCustom/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1149386202403844156> kapa.ai is still learning and improving, please let us know how it did by reacting below
adverse-sapphire
adverse-sapphire8mo ago
@kapa.ai how to fetch the api with different params
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To fetch the API with different parameters using the useCustom hook in refine, you can pass the new parameters to the refetch function. The refetch function accepts an object with the same properties as the useCustom hook. Here is an example of how you can do it:
const {{
isLoading,
data,
refetch,
}} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({{
url: `${{apiUrl}}/${{apiPaths.bankStatement.entries}}`,
method: 'get',
queryOptions: {{
enabled: false,
}},
config: {{
query: {{
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
}},
}},
errorNotification: (error: ApiErrorResponse | undefined) => {{
return {{
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
}};
}},
successNotification: false,
}});

// Fetch the API with different parameters
refetch({{
config: {{
query: {{
pageSize: newPageSize,
pageNumber: newPageNumber,
searchText: newSearchText,
// ...other parameters
}},
}},
}});
const {{
isLoading,
data,
refetch,
}} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({{
url: `${{apiUrl}}/${{apiPaths.bankStatement.entries}}`,
method: 'get',
queryOptions: {{
enabled: false,
}},
config: {{
query: {{
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
}},
}},
errorNotification: (error: ApiErrorResponse | undefined) => {{
return {{
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
}};
}},
successNotification: false,
}});

// Fetch the API with different parameters
refetch({{
config: {{
query: {{
pageSize: newPageSize,
pageNumber: newPageNumber,
searchText: newSearchText,
// ...other parameters
}},
}},
}});
In this example, refetch() is used to manually trigger the API call with new parameters. You can replace newPageSize, newPageNumber, newSearchText, etc. with your new values. For more information, you can refer to the useCustom documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCustom/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1149386202403844156> kapa.ai is still learning and improving, please let us know how it did by reacting below
adverse-sapphire
adverse-sapphire8mo ago
@kapa.ai refetch page
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To refetch a page using the useCustom hook in refine, you can use the refetch function that is returned by the hook. The refetch function allows you to manually trigger the API call whenever you need it. Here is an example of how you can do it:
const {{
isLoading,
data,
refetch,
}} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({{
url: `${{apiUrl}}/${{apiPaths.bankStatement.entries}}`,
method: 'get',
queryOptions: {{
enabled: false,
}},
config: {{
query: {{
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
}},
}},
errorNotification: (error: ApiErrorResponse | undefined) => {{
return {{
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
}};
}},
successNotification: false,
}});

// Refetch the page
refetch();
const {{
isLoading,
data,
refetch,
}} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({{
url: `${{apiUrl}}/${{apiPaths.bankStatement.entries}}`,
method: 'get',
queryOptions: {{
enabled: false,
}},
config: {{
query: {{
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
}},
}},
errorNotification: (error: ApiErrorResponse | undefined) => {{
return {{
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
}};
}},
successNotification: false,
}});

// Refetch the page
refetch();
In this example, refetch() is used to manually trigger the API call. You can call this function wherever you need to refresh the data. For more information, you can refer to the useCustom documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCustom/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1149386202403844156> kapa.ai is still learning and improving, please let us know how it did by reacting below
adverse-sapphire
adverse-sapphire8mo ago
@kapa.ai refetch a page with pagenumber
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To refetch a page with a specific page number using the useCustom hook in refine, you can pass the new page number to the refetch function. The refetch function accepts an object with the same properties as the useCustom hook. Here is an example of how you can do it:
const {{
isLoading,
data,
refetch,
}} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({{
url: `${{apiUrl}}/${{apiPaths.bankStatement.entries}}`,
method: 'get',
queryOptions: {{
enabled: false,
}},
config: {{
query: {{
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
}},
}},
errorNotification: (error: ApiErrorResponse | undefined) => {{
return {{
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
}};
}},
successNotification: false,
}});

// Refetch the page with a specific page number
refetch({{
config: {{
query: {{
pageNumber: newPageNumber,
// ...other parameters
}},
}},
}});
const {{
isLoading,
data,
refetch,
}} = useCustom<
BankStatementEntryListResponse,
ApiErrorResponse,
BankStatementEntrySearchRequest
>({{
url: `${{apiUrl}}/${{apiPaths.bankStatement.entries}}`,
method: 'get',
queryOptions: {{
enabled: false,
}},
config: {{
query: {{
pageSize: searchRequest.pageSize,
pageNumber: searchRequest.pageNumber,
searchText: searchRequest.searchText,
status: searchRequest.status,
fromDate: searchRequest.fromDate,
toDate: searchRequest.toDate,
bankUuid: searchRequest.bankUuid,
entryType: searchRequest.entryType,
}},
}},
errorNotification: (error: ApiErrorResponse | undefined) => {{
return {{
message: t('pages.bankStatement.entriesList.errorNotificationMessage'),
type: 'error',
description: error?.detailedMessage,
}};
}},
successNotification: false,
}});

// Refetch the page with a specific page number
refetch({{
config: {{
query: {{
pageNumber: newPageNumber,
// ...other parameters
}},
}},
}});
In this example, refetch() is used to manually trigger the API call with a new page number. You can replace newPageNumber with your desired page number. For more information, you can refer to the useCustom documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCustom/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1149386202403844156> kapa.ai is still learning and improving, please let us know how it did by reacting below