๐ง๐ต๐ฒ ๐๐๐ผ๐น๐๐๐ถ๐ผ๐ป ๐ข๐ณ ๐๐ฎ๐๐ฎ ๐๐ฒ๐๐ฐ๐ต๐ถ๐ป๐ด
Frontend fetching changed. It started with callbacks. Then it moved to Promises. Now we use strategy-based hooks.
Promise-based code is imperative. You tell the computer how to do every step. You manage loading states. You handle errors manually. You struggle with useEffect and useCallback. This creates too much boilerplate.
Strategy hooks are declarative. You describe what you need. The library handles the execution.
Libraries like alova offer specific hooks for business patterns:
- usePagination: handles lists and infinite scroll.
- useForm: manages form submissions.
- useAutoRequest: handles polling and focus refresh.
- useUploader: manages file uploads.
This approach offers three main benefits:
- It separates request logic from UI components.
- It works across React, Vue, and Svelte.
- It lets you combine different strategies.
Use strategy hooks if:
- You build standard CRUD pages.
- You work on medium or large projects.
- You target multiple platforms.
- You need memory and persistent caching.
Stick to plain fetch or axios if:
- You have a simple page with one request.
- Your request flow is highly custom.
- Your team already uses a stable tool.
- You handle low-level HTTP like WebSockets.
Declarative fetching saves time. It removes repetitive code. Pick the tool based on your project scale.
Source: https://dev.to/coderhu/the-evolution-of-fetch-libraries-from-promise-to-strategies-2ckh