Skip to content

Commit ced311c

Browse files
authored
Update composables.md (#2473)
Modifying data.value and error.value within watchEffect has the potential to trigger infinite re-renders.
1 parent ead027b commit ced311c

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/guide/reusability/composables.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,16 @@ export function useFetch(url) {
216216
const data = ref(null)
217217
const error = ref(null)
218218
219-
watchEffect(() => {
220-
// reset state before fetching..
221-
data.value = null
222-
error.value = null
223-
// toValue() unwraps potential refs or getters
224-
fetch(toValue(url))
219+
const fetchData = (dt) => {
220+
fetch(toValue(url))
225221
.then((res) => res.json())
226222
.then((json) => (data.value = json))
227223
.catch((err) => (error.value = err))
224+
}
225+
226+
watchEffect(() => {
227+
// reset state before fetching..
228+
fetchData(url)
228229
})
229230
230231
return { data, error }

0 commit comments

Comments
 (0)