1

I have a component that takes router urls params and trigger calls depending on these params.

Here's how I made it

const {
  data: data1,
  isLoading: isLoading1,
  isError: isError1,
} = useQuery({
  queryKey: [`test-${props.match.params.language}`],
  queryFn: () =>
    fetch(
      `${BASE_URL}${API_URLS.GET_QUIZ}/?languages=${props.match.params.language}&level=${props.match.params.difficulty}`
    ).then((response) => response.json()),
  enabled: props.match.params.source === "api",
});

const {
  data: data2,
  isLoading: isLoading2,
  isError: isError2,
} = useQuery({
  queryKey: [`test-${props.match.params.language}`],
  queryFn: () => {
    console.log("intoQuery");
    return fetch(
      `${BASE_URL}${API_URLS.TEST}/${props.match.params.language}`
    ).then((res) => res.json());
  },
  enabled: props.match.params.source === "intern",
});

useEffect(() => {
  console.log("data2", data2);
  console.log("data1", data1);
  if (props.match.params.source === "api") {
    setData(() => data1);
    setIsloading(isLoading1);
    setIsError(isError1);
  }
  if (props.match.params.source === "intern") {
    setData(() => data2?.questions || []);
    setIsloading(isLoading2);
    setIsError(isError2);
  }
}, [data1, data2]);

It works but I find that the code is redundant, and there is much boiterplate.

I would like to know if is possible to optimise it better and only return one {data, isLoading,IsError} object instead of having {data1, isLoading1,IsError1} and {data2, isLoading2,IsError2}

I very simple solution is to conditionally set the url.

But I would like to know it these is other ways

3
  • github.com/trojanowski/react-apollo-hooks/issues/120 Commented Nov 17, 2022 at 11:42
  • Here the queries should be triggered conditionally, and I think it's not the same package , I'm using : @tanstack/react-query Commented Nov 17, 2022 at 11:49
  • You overwrite the data in setData with each approach. Better to think about how to separate the two approaches instead of looking to combine queries, as they don't depend on each other. Look for suspend and lazy loading in react. Commented Nov 18, 2022 at 8:29

1 Answer 1

1
const { data, isLoading, isError } = useQuery({
  queryKey: ['test', props.match.params.language, props.match.params.difficulty],
  queryFn: () =>
    fetch(
      `${BASE_URL}${API_URLS.GET_QUIZ}/?languages=${props.match.params.language}&level=${props.match.params.difficulty}`
    ).then((response) => response.json())
});

if the props change, so will your query key, and it will trigger a new fetch.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Tk, great work on TSQ. Is there an article explaining why the shift to using queryKey over parameters? This will require a lot of refactoring for some codebases.
if you are referring to the object syntax { queryKey, queryFn, ...options } over querykey, queryFn, options, please see remove overloads in the v5 roadmap: github.com/TanStack/query/discussions/4252 please also note that the syntax has always been valid, so you can migrate today, and there is even an eslint rule that is auto fixable that will migrate for you: tanstack.com/query/v4/docs/eslint/prefer-query-object-syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.