Can I use useQuery with only initialData (no queryKey nor queryFn)?
              
              #8308
            
            
              
                Unanswered
              
          
                  
                    
                      tran-simon
                    
                  
                
                  asked this question in
                General
              
            Replies: 2 comments 2 replies
-
| I guess that you could actually go around the react-query itself. It even seems to me that it might be more performant. const synthesizeSuccess = (data: Doc): QueryObserverSuccessResult<Doc, Error> => ({
  data,
  error: null,
  isError: false,
  isPending: false,
  isLoading: false,
  isLoadingError: false,
  isRefetchError: false,
  isSuccess: true,
  status: 'success',
  dataUpdatedAt: 0,
  errorUpdatedAt: 0,
  failureCount: 0,
  failureReason: null,
  errorUpdateCount: 0,
  isFetched: true,
  isFetchedAfterMount: true,
  isFetching: false,
  isInitialLoading: false,
  isPaused: false,
  isPlaceholderData: false,
  isRefetching: false,
  isStale: false,
  refetch: (_opts?: RefetchOptions) => Promise.resolve(synthesizeSuccess(data)),
  fetchStatus: 'idle',
  promise: Promise.resolve(data),
});
const documents: Record<string, Doc> = {};
type DBDoc = {
  bar: string;
};
type Doc = {
  foo: number;
};
const useDocument = (id: string): UseQueryResult<Doc, Error> => {
  const defaultDocument = useMemo(() => {
    const doc = documents[id];
    if (doc) {
      return synthesizeSuccess(doc);
    }
    return null;
  }, [id]);
  const query = useQuery({
    queryKey: ['custom-doc', id],
    queryFn: async () => {
      return Promise.resolve({} as DBDoc);
    },
    select: (data: DBDoc): Doc => {
      // ... transform data
      return {} as Doc;
    },
    enabled: defaultDocument === null,
  });
  return defaultDocument !== null ? defaultDocument : query;
}; | 
Beta Was this translation helpful? Give feedback.
                  
                    1 reply
                  
                
            -
| if  If you don’t need different transformations per useQuery call, I wouldn’t use  | 
Beta Was this translation helpful? Give feedback.
                  
                    1 reply
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
hi
I'm creating a custom hook to get some data form an id.
I have some default data included in my app that I can return synchronously if the id matches.
Else, I need to fetch the data:
I would like my function to return a
UseQueryResultobject (so I can accessisLoadingin the case that the document is custom.Would this work?
I cannot put
initialDatadirectly in the customDocumentuseQuery, because the queryFn returns an object of a different type (that's why I need theselectfunction.initialDataneeds to be the type of the return value of thequeryFn.Beta Was this translation helpful? Give feedback.
All reactions