TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Data Fetching for Single-Page Apps

120 pointsby fagnerbrack10 months ago

10 comments

morbicer10 months ago
I would expect a bit more in-depth article from Martin Fowler website.<p>The shown implementation is naive and prone to bugs, for example a race condition. If this id is rapidly changed (say from 1-&gt;2), and the first request arrives last, you think you are looking at user 2 but you&#x27;ve loaded data for user 1.<p>const [user, setUser] = useState&lt;User | undefined&gt;();<p><pre><code> useEffect(() =&gt; { const fetchUser = async () =&gt; { const response = await fetch(`&#x2F;api&#x2F;users&#x2F;${id}`); const jsonData = await response.json(); setUser(jsonData); }; fetchUser(); }, [id]); </code></pre> If you don&#x27;t know what you&#x27;re doing, use Tanstack Query libs or read a better article.<p>- <a href="https:&#x2F;&#x2F;tanstack.com&#x2F;query&#x2F;latest" rel="nofollow">https:&#x2F;&#x2F;tanstack.com&#x2F;query&#x2F;latest</a><p>- <a href="https:&#x2F;&#x2F;maxrozen.com&#x2F;race-conditions-fetching-data-react-with-useeffect" rel="nofollow">https:&#x2F;&#x2F;maxrozen.com&#x2F;race-conditions-fetching-data-react-wit...</a>
评论 #41032239 未加载
评论 #41046464 未加载
评论 #41038146 未加载
评论 #41041968 未加载
ibash10 months ago
The most common mistake I see is putting data fetching in the view layer.<p>React hooks caused this because by default there’s no sensible place to fetch data.<p>So unless you know ahead of time, any complex single page ends up with complex and hard to track network requests.<p>The solution is really simple: pull data out of the view layer and make it a first class citizen.<p>—<p>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?
评论 #41035473 未加载
评论 #41035411 未加载
samradelie10 months ago
Bleugh. Who is this article for? (good bait from the poster) Anyone who&#x27;s done any kind of meaningful SPA dev knows this pattern does not scale &amp; for newbies, this is poison.<p>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.<p>Everyone who has commented on this is spot on.
tuyiown10 months ago
I am horrified. I didn&#x27;t see mentioned, proper data fetching _must_ happen before UI rendering if you want to have consistent behavior with classic HTML&#x2F;HTTP apps, e.g. the current page content stays displayed until the received html starts to be rendered (with the actual data for pure HTML). You loading feed back _has_ to happen from the interacted page in first intent.<p>If you want to have an after routing&#x2F;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.<p>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.
评论 #41035015 未加载
评论 #41033107 未加载
评论 #41033627 未加载
评论 #41035749 未加载
ramesh3110 months ago
The <i>how</i> is of course extremely simple. But <i>when</i> is the real question. You can do a full send on every route load, but at that point you might as well just do SSR. Dealing with stale data is a nightmare, but you want to have at least <i>some</i> client side inter-page caching to avoid redundant API calls. I&#x27;ve never found a perfect solution here.
beejiu10 months ago
The article sort of touches on this, but in my experience working with React the most important thing is to keep as much as you can declarative. When you start doing things in an imperative way, particularly when they are async, that&#x27;s when you run into problems. It&#x27;s one of the reasons I find GraphQL such a good fit.
knallfrosch10 months ago
The missing syntax highlighting (or the absence of <i>any</i> highlighting) makes it really hard to read.<p>Plus the article only refers to React, but it could have stayed a bit more abstract about the techniques.
FjordWarden10 months ago
I know that Martin Fowler is your God, and that my denigrations upon this divinity will not go unpunished, or that this guy is not Martin Fowler, but what has this guy ever build to convince me that he actually knows what he is talking about? Maybe I am missing that or something else, but here I read that &quot;Today most applications can send hundreds of requests for a single page&quot;, like one would hope there to be a sort of role within the software enterprise that would actively try to discourage people from being stupid like that.<p>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.
评论 #41033688 未加载
评论 #41033868 未加载
评论 #41032316 未加载
评论 #41032254 未加载
gr4vityWall10 months ago
I think it&#x27;s fascinating how data fetching has been a mostly solved problem in Meteor for years.<p>I understand why you&#x27;d use Tanstack Query or similar solutions most of the time. I wish the mental overhead of using them was smaller.
efilife10 months ago
Should have &quot;in react&quot; in the title