Conditional import/export of client/server versions of same code
Unanswered
Sloth bear posted this in #help-forum
Sloth bearOP
Hello all! I've been working in the React/Next.js after coming from Angular for years and am loving it. Am stuck on something and can't seem to get a straight answer from online.
Context: I've got two versions of the same hook,
Question: Is there some way to create a
If this is not possible (which is what I'm coming to suspect), is there an RFC for adding support for this? It feels like an amazing developer ergonomic improvement and honestly I'm kind of surprised it doesn't exist yet, considering how much it'd abstract away from consumers.
Context: I've got two versions of the same hook,
useClientTranslation
and useServerTranslation
which both return a t
function and an i18n
context. They both live in the directory i18n
and their file names are client.ts
and server.ts
respectively.Question: Is there some way to create a
index.ts
file that conditionally imports and then exports each of those based on whether it's currently in a server-only or client-only context under the name useTranslation
, so that consuming components can be entirely agnostic to which one is being imported?If this is not possible (which is what I'm coming to suspect), is there an RFC for adding support for this? It feels like an amazing developer ergonomic improvement and honestly I'm kind of surprised it doesn't exist yet, considering how much it'd abstract away from consumers.