Common Hooks
Overview
We can develop custom React hooks like useCustomAPIMutationHook
to fine-tune the application API interactions. The custom hooks allow us to control how data is fetched and managed, aligning it perfectly with our requirements.
Configuration
This custom hook, useCustomAPIMutationHook
, simplifies API calls for mutation operations in React applications using react-query
. It allows parameter and body customization for requests to a specified URL, seamlessly managing loading indicators and states. This hook streamlines API interaction and state management within components.
Create useCustomAPIMutationHook
under the following path:
micro-ui-internals/packages/libraries/src/hooks/useCustomAPIMutationHook.js
import { useQueryClient, useMutation } from "react-query";
import { CustomService } from "../services/elements/CustomService";
const requestCriteria = [
"/user/_search", // API details
{}, //requestParam
{data : {uuid:[Useruuid]}}, // requestBody
{} , // privacy value
{ // other configs
enabled: privacyState,
cacheTime: 100,
select: (data) => {
// format data
return _.get(data, loadData?.jsonPath, value);
},
},
];
const mutation = Digit.Hooks.useCustomAPIMutationHook(...requestCriteria);
while mutating use the following format
mutation.mutate({
params: {},
body: { "payload": {
// custom data
}
}},
{
onError : ()=> { // custom logic},
onSuccess : ()=> { // custom logic}
}
);
const useCustomAPIMutationHook = ({ url, params, body, config = {}, plainAccessRequest, changeQueryName = "Random" }) => {
const client = useQueryClient();
const { isLoading, data, isFetching, ...rest } = useMutation(
(data) => CustomService.getResponse({ url, params: { ...params, ...data?.params }, body: { ...body, ...data?.body }, plainAccessRequest }),
{
cacheTime: 0,
...config,
}
);
return {
...rest,
isLoading,
isFetching,
data,
revalidate: () => {
data && client.invalidateQueries({ queryKey: [url].filter((e) => e) });
},
};
};
export default useCustomAPIMutationHook;
Override Hook
To override the useCustomAPIMutationHook
, create a new custom hook with the desired enhancements. When considering overriding useCustomAPIMutationHook
, identify specific modifications required, such as adding additional parameters or altering data processing logic. Create a new custom hook, preserving the original functionality while incorporating these modifications to suit specific application needs.
We need to replace the usage of the original hook throughout the codebase with the new custom hook. This ensures that wherever the original hook is used, the modified functionality of the custom hook is applied instead.
There is a hook called useCustomMDMSV2
, which is used to get data from a Multi-Domain Master Service (MDMS) API. This hook makes it easier to request data, handle load, and prepare the received data for React applications.
useCustomMDMSV2
Create the hook under the following path:
micro-ui-internals/packages/modules/Sample/src/hooks/useCustomMDMSV2.js
export const useCustomMDMSV2 = ({ tenantId, schemaCode, select, changeQueryName = "Random",filters={},config={} }) => {
const requestCriteria = {
url: "/mdms-v2/v2/_search",
body: {
tenantId,
MdmsCriteria: {
// tenantId, //changing here to send user's tenantId always whether stateId or city
tenantId: Digit.ULBService.getCurrentTenantId(),
filters: filters,
schemaCode: schemaCode,
isActive: true,
limit: 100
},
},
config: {
select: select
? select
: (response) => {
//mdms will be an array of master data
const { mdms } = response;
//first filter with isActive
//then make a data array with actual data
//refer the "code" key in data(for now) and set options array , also set i18nKey in each object to show in UI
const options = mdms?.map((row) => {
return {
i18nKey: Digit.Utils.locale.getTransformedLocale(`${row?.schemaCode}_${row?.data?.code}`),
...row.data,
};
});
return options;
},
...config
},
changeQueryName,
};
const { isLoading, data } = Digit.Hooks.useCustomAPIHook(requestCriteria);
return { isLoading, data };
};
Last updated
Was this helpful?