How can we build a component library using RSCs that allows the user to BYO data fetching function? What is the alternate approach where a context provider would otherwise be used? #66790
Replies: 2 comments
-
NextJS's documentation does mention context providers here: But the answer is just add to make things client components. I'm really looking for configurability as it relates to components that can be rendered server side. |
Beta Was this translation helpful? Give feedback.
-
One of the downsides of the approach I've mentioned is that it wouldn't have the same flexibility that a context provider has - in that there can only be one of them. ie. where I could have a different context provider for pages Whereas in the solution I outlined there can only be one configuration provider, and attempts to set it elsewhere would just clobber the previous configuration. Not a relevant concern for my use case, but worth mentioning. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I maintain a library called react-github-permalink - where the basic usage is you give it a github permalink and it displays a code block:
Its relevant implementation looks like this:
https://github.com/dwjohnston/react-github-permalink/blob/b4ed5a9bca5e085ca313470ad9e8fd4f570c2721/src/GithubPermalink/GithubPermalink.tsx#L13-L25
(Not sure why the code block isn't displaying, is that for issues only?)
Note two things:
This approach is working well, it's easy to configure at a global level and is convenient to use - but this won't work for an RSC.
Given that the primary usage of this component is for blogs we might as well be getting the benefits of static rendering.
Is there a guide or a first class approach for doing dependency injection like this?
I have done a quick experiment and doing something like this:
does seem to work.
I couldn't quite get typedi to work, but this approach is very much in the same vein.
Is there documentation somewhere as it relates to alternatives to context?
Beta Was this translation helpful? Give feedback.
All reactions