sunny-green
sunny-green12mo ago

useCustom types of response

How can i define the type of the data object result of the useCustom webhook?
5 Replies
kapa.ai
kapa.ai12mo ago
Hi @jaoc1811. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In the useCustom hook, you can define the type of the data object result by specifying it as a generic parameter. The useCustom hook has several type parameters, including TQueryFnData and TData. TQueryFnData is the result data returned by the query function and TData is the result data returned by the select function. If TData is not specified, the value of TQueryFnData will be used as the default value. Both of these types extend BaseRecord. Here is an example of how you can define the type of the data object result:
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, PostUniqueCheckResponse is the custom type defined for the data object result. For more information, you can refer to the useCustom documentation (https://refine.dev/docs/data/hooks/use-custom). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai12mo ago
<@110864105035808768> kapa.ai is still learning and improving, please let us know how it did by reacting below
sunny-green
sunny-greenOP12mo ago
@kapa.ai how can I assign the data from useCustom in a useState?
kapa.ai
kapa.ai12mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- You can assign the data from useCustom to a state variable using the useState hook from React. Here's an example of how you can do it:
import { useState, useEffect } from "react";
import { useCustom, useApiUrl } from "@refinedev/core";

interface PostUniqueCheckResponse {
isAvailable: boolean;
}

const apiUrl = useApiUrl();

const YourComponent = () => {
const [customData, setCustomData] = useState<PostUniqueCheckResponse | null>(null);
const { data, isLoading } = useCustom<PostUniqueCheckResponse>({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
});

useEffect(() => {
if (data && !isLoading) {
setCustomData(data);
}
}, [data, isLoading]);

// rest of your component
}
import { useState, useEffect } from "react";
import { useCustom, useApiUrl } from "@refinedev/core";

interface PostUniqueCheckResponse {
isAvailable: boolean;
}

const apiUrl = useApiUrl();

const YourComponent = () => {
const [customData, setCustomData] = useState<PostUniqueCheckResponse | null>(null);
const { data, isLoading } = useCustom<PostUniqueCheckResponse>({
url: `${apiUrl}/posts-unique-check`,
method: "get",
config: {
headers: {
"x-custom-header": "foo-bar",
},
query: {
title: "Foo bar",
},
},
});

useEffect(() => {
if (data && !isLoading) {
setCustomData(data);
}
}, [data, isLoading]);

// rest of your component
}
In this example, useState is used to create a state variable customData and a setter function setCustomData. The useEffect hook is then used to update customData whenever data changes and is not loading. Please note that you should handle the loading and error states appropriately in your component. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai12mo ago
<@110864105035808768> kapa.ai is still learning and improving, please let us know how it did by reacting below

Did you find this page helpful?