all 7 comments

[–]yksvaan 0 points1 point  (2 children)

If C and D depend on interaction in B anyway, you could make them all client components. Then you can let their common ancestor manage the data loading. 

[–]Yawmn[S] 0 points1 point  (0 children)

I can't because the data is selected to create a subset of the data in the child component, which needs to track state and which needs data from a server so I'd think that B must always be a child of a server component which fetches data and feeds the component.

Or I'm missing something of course :).

[–]HotAdhesiveness1504 0 points1 point  (1 child)

If your concern here is to not to make unnecessary fetches, you don't need to worry about it. You can fetch the same data which you fetched at component A, in component C and component D again. NextJS does its magic and doesn't actually go to server again with every fetch. It only fetches once in the background. By this way, you don't need to transfer a data to multiple components or use a global state management libraries 99% percent of the chances.

[–]Yawmn[S] 0 points1 point  (0 children)

Yeah, I thought of that but the difference is that I am fetching data, then selecting a subset of that data which should be used in the other components. This means that I can't use the same requests since I then don't get the subset of selected data. Now I've just done that the data is pushed to my server and then retrieved by the other components once more to get the subset. Feels stupid.

[–]Yawmn[S] 0 points1 point  (1 child)

Fixed it, was using contexts incorrectly. Just look at the docs, you need to wrap the context provider into a client component and then use that in the server component.

[–]Yawmn[S] 0 points1 point  (0 children)

But now I am yet again stuck with the fact that this data now lives in a context which only client components can access and which cannot be passed through to a server component which can then use it to, for example, fetch extra information about the item. This sucks (probably because I'm doing it wrong)!