Replies: 1 comment
-
I'm in the middle of something similar to the stated problem. We are using the latest next.js with redux but we are questioning if RSC adds more complexity to how the mental model of redux operates. In other words, you can make it work just like you can hammer a square into a circle. This boils down to:
In your RSC, fetch your data from the db and pass it to your components to render on the server. If you can get calculate the 'like' in the server you'd want to do that there, and pass the final 'state' to the components to render them completely. Assuming that that is not what you want and you want the 'like' to be dynamic, then that component will necessarily need to be a client component that get's it initial 'like' data from the server and then proceeds to periodically fetch data on the client to update the 'like' value. But this negates your 'single api call' to do both. Which is alright, since you'd want to split up the logic/calls for fetching posts and fetching likes. Fetch the 'post' data on the server and render your If you have to use a state management library, and you fetch data from the server to store in a redux store, you're going to be wrestling a bit until you find a pattern that works for you, and well, mostly just works (hydration issues). In general, I think this is a progressively hard problem: fetch the data in the server and store it on the client. I'd start with useContext to get a feel for how components are connected and reacting to each other. But as soon as ComponetA state in page I haven't found a pattern that I like but I do have something working for now. The concepts of rendering components on the server and storing state on the client are appear orthogonal from thirty thousand feet. But on closer inspection, there is overlap and there are solutions to it but like I said initially, you can make it work just like you can hammer a square into a circle. N.B. |
Beta Was this translation helpful? Give feedback.
-
Problem
I am creating a simple discussion board with bunch of posts and a 'like' feature like youtube or facebook. I am using Nextjs app dir for the framework and RTK Query for data-fetching and data-caching from the server.
I want to fetch the data of a post from the server using 'fetch' because I want to build the page ahead of the time. At the same time, I want to mutate some information of the post data, such as mutating the count of 'like.' (with optimistic update preferrably)
How would you architect or manage this use case in the most elegant way possible? Let's assume I am working with a single api call that includes post content and the count of likes.
Possible ways to solve
Notes
Please feel free to give any suggestions or feedback. I am sure this is not RTK Query only problem, but rather the new wave of RSC and Nextjs app dir problem. I understand where they are heading, but I just don't want to give up the power of RTK Query or other client-side libraries, really.
Cheers.
Beta Was this translation helpful? Give feedback.
All reactions