useQuery & undefined data #1331
Replies: 12 comments 28 replies
-
Have you tried using |
Beta Was this translation helpful? Give feedback.
-
Then if you want to show loader even though you have data you can use |
Beta Was this translation helpful? Give feedback.
-
I found it a bit tricky because usually I prefer to use |
Beta Was this translation helpful? Give feedback.
-
When you need to load data once e.g. on the application start, I personally like to use "Gate" component. The role of the gate is to stop futher rendering as long as data is not loaded yet. When data is finally loaded I use context&customHook to read my initial data. At this point Im sure that my data will not be undefined. You can check a PoC here: |
Beta Was this translation helpful? Give feedback.
-
Couldn't agree more with the issue of the undefined/null checks polluting code base. |
Beta Was this translation helpful? Give feedback.
-
When supplying an const useTheme = () => {
return useQuery(
['theme'],
() => getTheme()
{
initialData: DEFAULT_THEME,
},
) as QueryObserverSuccessResult<Theme, never>;
};
|
Beta Was this translation helpful? Give feedback.
-
Any update on this? I don't really understand how |
Beta Was this translation helpful? Give feedback.
-
Not sure if I am asking at the right place, but I am using |
Beta Was this translation helpful? Give feedback.
-
When having multiple queries, you can't destructure because the repeated data fields. However, that means you can't preset the data field to [], hence typescript thinks data is undefined.
Seems like the only real option would be to add another field like nedgrady's solution
|
Beta Was this translation helpful? Give feedback.
-
I have custom hook with staleTime = Infinity
|
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
Anyone knows how I can tell react query that my data is prefetched on the server side and thus will always have initial value on the client side? So I am using Remix and I have this loader:
then on the client side I do:
but the data is of type |
Beta Was this translation helpful? Give feedback.
-
Hi, I've been using react-query for some time now, and so far enjoying it. However, one thing that I find odd is data being undefined from useQuery. For example, let's look at the following code.
const { data } = useQuery("active-events", () => getEventsAsync());
In the above code, getEventsAsync() will always return an array or throw an error.
Here, data is supposed to be an array, but since the type is union with undefined I'll need to do the following before consuming it in my component further,
const events = data ?? [];
In case of errors, loading etc, we already have flags from useQuery to check them, is there a better way to avoid undefined type be scattered across my code?
In short, I want to avoid undefined & unnecessary null checks in my codebase as they can cause a lot of headaches later on. How do you guys approach this issue?
Edit: Even if I pass initialData to useQuery as an empty array, the type of data will still remain as
MyEvent[] | undefined
, and I'll still need to handle null check to tell typescript that data can never be undefined.Beta Was this translation helpful? Give feedback.
All reactions