-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Abstract the router requirement #858
Comments
@RobinKnipe: Any ideas on how we can do this in practice? - I'd rather not drop support for BTW, how does Next.js do this? Does it magically intercept normal |
Next.js has its own 📚 https://nextjs.org/docs/app/api-reference/components/link It's used in much the same way <Link href="/dashboard">Dashboard</Link> // Navigate to /about?name=test
<Link
href={{
pathname: '/about',
query: { name: 'test' },
}}
>
About
</Link> It does have some differences though; it is intended only for internal links and throws a warning if used for external ones.
|
In that case, it's the same as it's always been. I suppose the question is can we detect the context we are in? i.e. A react-router or a next-router? (Perhaps via a literal React 'Context'?) If we can do that, we can simply switch between the relevant upstream Link components in our own Link component. |
I believe Next has a build phase - it attempts to do SSG and the necessary processing for asset creation (CSS/JS/etc) - so you would know then whether the context were Next or not. |
Next seems to detect it's own context here: https://github.com/vercel/next.js/blob/6ac4fd6ef84c4b0367ebfd122d88d4af3cd146fb/packages/next/src/client/link.tsx#L313 |
Similar code in react-router: https://github.com/remix-run/react-router/blob/main/packages/react-router-dom/index.tsx#L964 |
Another possibility for achieving this could be using React Context. I'm imagining something like a This could be simpler implementation-wise and more configurable for consumers of this library because you may not want every link in the application to be handled by React Router or Next.js. This also means you don't need to require either react-router or Next.js as a dependency at all, you just let the library consumer pass you the component they want you to render. |
Hi @SamChatfield. That's an interesting idea, thanks. If I'd thought of that a month or so ago, I probably would have tried to go with something like that as a first version. There are some complications, in that the Link components that are provided by Next.js and react-router have different APIs. We also, don't only interact with the router via the Link component, but rather make use of their hooks. (e.g. That said, it may be possible to have some dedicated packages for each framework. (e.g. Anyway, I think I'm pretty close with this. Current status is here: #1039 (comment) My approach has been to re-purpose the |
Currently there is a hard dependency on
react-router
which means the project can't be used in other routing scenarios (e.g. Next.JS). By abstracting the routing, the project could be made to work with other SSR environments.The text was updated successfully, but these errors were encountered: