Performance: Composable for API calls (useFetch pattern)
MEDIUM IMPACT
This pattern affects page load speed and interaction responsiveness by managing when and how API data is fetched and rendered.
import { ref } from 'vue'; export function useFetchUser() { const user = ref(null); const loading = ref(false); const error = ref(null); async function fetchUser() { loading.value = true; try { const res = await fetch('https://api.example.com/user'); user.value = await res.json(); } catch (e) { error.value = e; } finally { loading.value = false; } } return { user, loading, error, fetchUser }; } // Components import and reuse this composable
export default { data() { return { user: null }; }, async mounted() { const res = await fetch('https://api.example.com/user'); this.user = await res.json(); } }; // Repeated in every component needing user data
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Independent fetch in each component | Multiple redundant DOM updates | Multiple reflows per fetch | High paint cost due to repeated updates | [X] Bad |
| Composable useFetch pattern with shared state | Single DOM update per data change | Single reflow per data update | Lower paint cost with minimal updates | [OK] Good |