TanStackT
TanStack3y ago
3 replies
primary-violet

useMemo and queryFn

So I have a fairly simple query function, shown below. I was wondering if const body = await response.json() and zResponse.parse(body); would run on every render? Would it make sense to wrap them in a useMemo with response as the dependency? This queryFn is in a custom hook.
const queryFunction = async ({ numDocumento, numPoliza, first }: TRequest): Promise<TResponse> => {
  const response = await fetch('http://someUrl.com/endpoint1/', {
    method: 'POST',
    body: JSON.stringify(
      zRequest.parse({
        numPoliza,
        numDocumento,
        first,
      })
    ),
  });

  if (!response.ok) {
    throw new Error('Response was not ok');
  }

  const body = await response.json();

  zResponse.parse(body);

  return body;
Was this page helpful?