Should select on Suspense Query throw errors? #9957
-
|
Dear TanStack team, I have a question connected to React Suspense Query Lets look at the following example: type ResolvedDataType = Record<number, string>;
const selectItemById = (id: number) => (data: ResolvedDataType) => {
const item = data[id];
if (!item) {
throw new Error('Item not found');
}
return item;
};
const ComponentA = () => {
const externallyControlledId = 2;
const { data } = useSuspenseQuery({
queryKey: ['query-key'],
queryFn: async () => Promise.resolve<ResolvedDataType>({ 1: 'one' }),
select: selectItemById(externallyControlledId),
});
return <p>{data}</p>;
};I would expect the But in this case, even tho Typescript tells me Also, rendering of the component will not be canceled by the throw error in the Is my assumption right, that component |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
@TkDodo I found this discussion and PR you made. Is it related to this issue? The docs addition doesn’t mention |
Beta Was this translation helpful? Give feedback.
-
|
if you have a query/packages/query-core/src/queryObserver.ts Lines 547 to 552 in f15b7fc then, the react integration will throw that error to the error boundary: query/packages/react-query/src/useBaseQuery.ts Lines 124 to 142 in f15b7fc you can clearly see that if you take your own example and put it in a sandobx: https://stackblitz.com/edit/tanstack-query-zmlk4cr7?file=src%2Findex.tsx&preset=node |
Beta Was this translation helpful? Give feedback.
if you have a
selectError, we will put the observer into error state:query/packages/query-core/src/queryObserver.ts
Lines 547 to 552 in f15b7fc
then, the react integration will throw that error to the error boundary:
query/packages/react-query/src/useBaseQuery.ts
Lines 124 to 142 in f15b7fc