7 ms·
"I don't recommend having components responsible for querying data because their responsibility is already in presenting the data. More accurately, components n
by riogordo2go 3y ago
"I don't recommend having components responsible for querying data because their responsibility is already in presenting the data. More accurately, components need only receive data from view models, format it, and output it to the screen. Instead, I advise you to follow either of the three following ways:
Render the data on the server and pass it to components as a view model, as shown in this guide.
Use a state management library that supports dispatching actions (e.g. Redux or MobX), deriving view models from the state, and passing them to components as properties."
This is opposite to what Next.js new React Server Components stuff is advocating for in the new app/ dir structure. It expects every component to fetch the required data, and just cache any duplicate calls. It's probably good for a Serverless / Edge deployment but I much rather use a MobX store that takes care of state, fetching and mutations.
- kipple 3y agoI came from a centralized store background — and honestly I'm falling in love with the decentralized data responsibilities. Between React Query (fetch batching & caching) and Jotai (decentralized state) I love that every component can just take care of itself. No more prop-drilling, everyone just declares their dependencies and takes care of their own.
- eurasiantiger 3y agoThat sounds like absolute hell to dig through once you hit N+1 queries.
- bakugo 3y agoYeah, this is the kind of thing that seems elegant and convenient when the requirements are simple, but completely falls apart as soon as any complexity is involved.
- sli 3y agoThis tracks for me as well. Very quickly, you start having to manage 10, 15, 20, or more places where data in a store is being manipulated from outside the store and it quickly becomes unmanageable on anything beyond a toy-sized application. It almost feels like a rite of passage to decentralize like that and then start to pull back when it becomes a nightmare to debug and maintain.
- MrJohz 3y agoThat's the purpose of the in-app request caching layer. Another way to see it is that you do have state in a separate store, it's just that you have one piece of state for each resource that can be requested. So it's not that all the components are requesting the /users resource, it's more that all the components a requesting the value of the /users store, and the /users store fetches the data once and determines whether the data is loading, present, needs to be updated, etc.
- bayesian_horse 3y agoThat caching layer sounds like a footgun. RTK Query does something similar, offering multiple ways to initiate, update and use the data of the queries.
- deleted 3y ago[deleted]
- brigadier132 3y agoIt all sucks. The grass is not greener on the other side. Just wait for the usEffect hell that awaits you.
- super256 3y agoIf you end up with useEffect hell, you are coding wrong. As OP said: it deduplicates and caches requests, there is no need to use useEffect. Here is some example code, and imo it‘s really greener than anything else. async function getData() { const res = await fetch('https://api.example.com/...'); if (!res.ok) { throw new Error('Failed to fetch data'); } return res.json(); } export default async function Page() { const data = await getData(); return <main></main>; }
- tuukkah 3y agoWhere's the caching in this example?
- panragon 3y agoHandled under the hood by Next 13. To be honest, they probably could have done something to make that a bit more obvious when looking at the code - it probably wouldn't hurt to throw in a simple 'cache' keyword or something.
- tuukkah 3y agoThis is the documentation I could find although it's not for 13 but Beta: https://beta.nextjs.org/docs/data-fetching/caching https://beta.nextjs.org/docs/data-fetching/caching They say there's a new `cache` function in React and they have patched `fetch` to use it by default (for GET requests). Does this include a solution to the N+1 queries issue (container element requests a list of items, then item elements each request their details)? I see you can do pre-fetching, perhaps those can be batched. EDIT: Found an example that uses DataLoader by "caching the cache". The src/api.ts module exports a cached DataLoader for characters, and src/components/CharacterAvatar.tsx imports and calls it during render simply like this: const character = await characters().load(id); https://github.com/AndrewIngram/next-rick-morty https://github.com/AndrewIngram/next-rick-morty
- gloryjulio 3y agoThe problem is the moment you need to do complex combination and computation from multiple async sources, it's game over. It's the repeating of the history of flux -> redux back then all over again. Further more, what happens when you need to share this computation across multiple components?
- kipple 3y agoFor all of that: Jotai
- gloryjulio 3y agoYes the point is that in this case the complexity is not by choice. If u have the business needs then u have to deal with them
- kipple 3y agoRight, you put the complexity inside the Jotai
- ikekkdcjkfke 3y agoSeparation of concerns is nice
- TheFragenTaken 3y agoThis is probably very tinfoil-haty, but I can't help but wonder how much of this recommendation is a perverse incentive to sell edge compute on Vercel's platform. In general I am very wary of VC-funded open source, like Next.js.
- epolanski 3y agoThey poached pretty much the entire react team in order to bet on: - react being the most popular FE library for time to come - gain the user's mindshare as react being now a Vercel product (something which is becoming increasingly true) - users attracted by react and next eventually becoming Vercel customers. They have also tried or employed several other major library authors in order to position themselves uniquely at the crossroads of major FE software and thus be uniquely positioned to transform those users again in Vercel customers. I don't judge any of this negatively or positively but that's the case I see.
- kevinak 3y agoThis is just a meme, an overwhelming majority of the React team ar still at Meta.
- epolanski 3y agoA very large chunk of the longest serving and more important jumped ship though.
- KyeRussell 3y agoMore important? Or more famous?
- azangru 3y ago> They poached pretty much the entire react team I remember they got Sebastian :-) Who else?
- 3y ago
- bayesian_horse 3y agoSomething like React Query and other hooks based approaches are fine and very useful for certain scenarios. At the end you always need a hook (or something very similar). But it can lead to defining asynchronous workflows through the nesting of components, controlled by their mounting/unmounting and other state. I much rather have something like RTK Query which lets me both use it in a hook for simplicity and in Thunks or Sagas for more complex things.
- janekm 3y agoThat sounds to me like it'd cause all sorts of trouble in the real world (authentication, changes to endpoint URLs, difficulty to agglomerate data), and not surprisingly the Next.js documentation states `For now, if you need to fetch data in a Client Component, we recommend using a third-party library such as SWR or React Query.`