Last active
August 8, 2022 14:29
-
-
Save frontsideair/8c87fb052bdec0bbff75ace346c27272 to your computer and use it in GitHub Desktop.
Next.js Link to Remix Link migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Link as RemixLink } from "@remix-run/react"; | |
import React from "react"; | |
type RemixLinkProps = Parameters<typeof RemixLink>[0]; | |
export type LinkProps = { href: RemixLinkProps["to"] } & Omit< | |
RemixLinkProps, | |
"to" | |
>; | |
// This components exists as a migration path from Next.js to Remix. | |
export default function Link({ href, children, ...props }: LinkProps) { | |
const link = <RemixLink to={href} {...props} />; | |
if (React.isValidElement(children) && children.type === "a") { | |
return React.cloneElement(link, { ...children.props, ...link.props }); | |
} else { | |
throw new Error("Link children must be an <a> element"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment