Data Fetching for Single-Page Apps
Efficient data fetching in single-page applications is crucial for responsiveness. Key patterns include Asynchronous State Handler, Parallel Data Fetching, Fallback Markup, Code Splitting, and Prefetching. Optimizing data fetching enhances application performance universally.
Read original articleIn the realm of single-page applications, fetching data efficiently is crucial for maintaining responsiveness and user experience. This article discusses five key patterns to aid in this process. The Asynchronous State Handler manages data queries with meta-queries, while Parallel Data Fetching reduces wait times. Fallback Markup provides fallback displays, Code Splitting loads necessary code only, and Prefetching gathers data in advance to reduce latency. The discussion emphasizes the importance of optimizing data fetching to enhance application performance. The article also touches on the increasing number of requests modern applications make and the strategies employed to make pages load progressively for a better user experience. It delves into React concepts like components, useState, and useEffect hooks, illustrating how these are fundamental in building dynamic UIs. The examples provided are not limited to React but are applicable across various frontend frameworks, emphasizing the universal nature of the discussed patterns in frontend development.
Related
- Several commenters criticize the article for being too simplistic and prone to bugs, suggesting the use of more robust solutions like Tanstack Query.
- There is a consensus that data fetching should be handled outside the view layer to avoid complexity and race conditions.
- Some commenters feel the article is too focused on React and should have been more abstract and framework-agnostic.
- Others emphasize the importance of declarative programming and proper data fetching before UI rendering for consistent behavior.
- There are calls for more in-depth and high-level discussions on data fetching patterns and techniques.
The shown implementation is naive and prone to bugs, for example a race condition. If this id is rapidly changed (say from 1->2), and the first request arrives last, you think you are looking at user 2 but you've loaded data for user 1.
const [user, setUser] = useState<User | undefined>();
useEffect(() => {
const fetchUser = async () => {
const response = await fetch(`/api/users/${id}`);
const jsonData = await response.json();
setUser(jsonData);
};
fetchUser();
}, [id]);
If you don't know what you're doing, use Tanstack Query libs or read a better article.- https://tanstack.com/query/latest
- https://maxrozen.com/race-conditions-fetching-data-react-wit...
React hooks caused this because by default there’s no sensible place to fetch data.
So unless you know ahead of time, any complex single page ends up with complex and hard to track network requests.
The solution is really simple: pull data out of the view layer and make it a first class citizen.
—
An exercise: imagine you’re building a tui instead of a web app, but you’re forced to use the exact same code for app state and data fetching… what would that code look like?
Instead of talking high-level framework agnostic code design and the multitudes of ways to fetch data (route based, state stores, authentication hooks, subscriptions, model composition , to name a few ); author goes deep into outdated React anti-patterns.
Everyone who has commented on this is spot on.
If you want to have an after routing/rendering loading feedback, such as skeleton or so, you still can do it, but it will be opt-in, not an after thought of savage data fetching and rendering that really might happens as the application gains features and diverts from the simple POC patterns.
Proper data fetching and rendering cannot happen without a router. Remix solved this with their updated react-router, I know that this router has a bad rep with breaking changes, but they finally landed the implementation that neatly cover most if not all the use cases for routing with dynamic code imports and data fetching.
Plus the article only refers to React, but it could have stayed a bit more abstract about the techniques.
Hey, at least when DHH jumps on the third rail of the hypermedia bandwagon I can laugh about the 500ms delay of a dropdown to show a calendar in his mail app, and I respect him for that.
I understand why you'd use Tanstack Query or similar solutions most of the time. I wish the mental overhead of using them was smaller.