stormy-gold
stormy-gold10mo ago

useList disable

Is it possible to disable useList based on prop value
2 Replies
kapa.ai
kapa.ai10mo ago
Hi @Joe Gaffney. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The useList hook in Refine does not have a direct property to disable it based on a prop value. However, you can conditionally call the useList hook based on a certain prop value. Please note that hooks cannot be called conditionally directly inside a component or another hook. They must always be called at the top level of the React function. To conditionally call a hook, you can create a custom hook and call it based on your condition. Here's an example:
import { useList } from "@refinedev/core";

const useConditionalList = (resource, condition) => {
if (condition) {
return useList({ resource });
}
return { data: null, isLoading: false, isError: false };
}

const MyComponent = ({ condition }) => {
const { data, isLoading, isError } = useConditionalList("myResource", condition);
// rest of your component
}
import { useList } from "@refinedev/core";

const useConditionalList = (resource, condition) => {
if (condition) {
return useList({ resource });
}
return { data: null, isLoading: false, isError: false };
}

const MyComponent = ({ condition }) => {
const { data, isLoading, isError } = useConditionalList("myResource", condition);
// rest of your component
}
In this example, useConditionalList is a custom hook that wraps the useList hook. It only calls useList if the condition is true. If the condition is false, it returns an object with the same shape as useList but with no data and no loading or error state. Please note that this is a simplified example and you might need to adjust it based on your needs. For more information about useList, you can check the official documentation (https://refine.dev/docs/data/hooks/use-list). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai10mo ago
<@422289470487003153> kapa.ai is still learning and improving, please let us know how it did by reacting below