useCustom on a click event, you would typically use it inside a function that is triggered by the click event. However, please note that hooks cannot be called inside callbacks or loops, they must be called at the top level of a React function component or a custom hook.Note: you can ask me a follow up question by @ mentioning me again Note: you can ask me a follow up question by @ mentioning me again useCustom hook when the button is clicked. Here's an example:useCustom hook is called at the top level of the component, but it only sends a request when the state is true. The function sets to true, triggering the useCustom hook.Note: you can ask me a follow up question by @ mentioning me again useCustom hook does not take a second argument. The hook should be called with a single argument, an object that contains the configuration for the request.mutate function returned by useCustom, which sends the request. The response from the request is then stored in the data state variable.Note: you can ask me a follow up question by @ mentioning me again useCustom hook does not return a mutate function. It returns a object, which includes properties like data and isLoading.useCustomMutation hook instead. This hook does return a mutate function that you can call to perform the mutation.useCustomMutation:mutate function returned by useCustomMutation, which sends the request.Note: you can ask me a follow up question by @ mentioning me again buttonClickedbuttonClickedhandleClickhandleClickhandleClickQueryObserverResultimport { useCustom, useApiUrl } from "@refinedev/core";
function MyComponent() {
const apiUrl = useApiUrl();
const handleClick = () => {
const { data, isLoading } = useCustom({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
});
}
return (
<button onClick={handleClick}>Click me</button>
);
}import { useCustom, useApiUrl } from "@refinedev/core";
import { useState } from 'react';
function MyComponent() {
const apiUrl = useApiUrl();
const [buttonClicked, setButtonClicked] = useState(false);
const { data, isLoading } = useCustom({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
}, buttonClicked);
const handleClick = () => {
setButtonClicked(true);
}
return (
<button onClick={handleClick}>Click me</button>
);
}import { useCustom, useApiUrl } from "@refinedev/core";
import { useState } from 'react';
function MyComponent() {
const apiUrl = useApiUrl();
const [buttonClicked, setButtonClicked] = useState(false);
const [data, setData] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const { mutate } = useCustom({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
});
const handleClick = async () => {
setIsLoading(true);
const response = await mutate();
setData(response);
setIsLoading(false);
}
return (
<button onClick={handleClick}>Click me</button>
);
}import { useCustomMutation, useApiUrl } from "@refinedev/core";
import { useState } from 'react';
function MyComponent() {
const apiUrl = useApiUrl();
const [buttonClicked, setButtonClicked] = useState(false);
const { mutate } = useCustomMutation();
const handleClick = () => {
mutate({
url: `${apiUrl}/categories`,
method: "post",
values: {
title: "New Category",
},
});
setButtonClicked(true);
}
return (
<button onClick={handleClick}>Click me</button>
);
}