Vue · Middle · Question 22 of 75

How should a composable accept arguments that might be refs, getters or plain values?

export function useFetch(url: MaybeRefOrGetter<string>) {
  watchEffect(() => load(toValue(url)));
}

Explanation

Calling toValue inside watchEffect (or a watch getter) tracks the argument, so useFetch(() => /api/${id.value}) re-fetches when id changes.

Further reading

Problem with a question

Vue · Middle · question 22

Test yourself

Take the Vue · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz