49
loading...
This website collects cookies to deliver better user experience
Link
component is exported from next/link
package. Button
with next's Link
and provide a passHref
prop so that it forwards href to the rendered anchor tag for proper semantics and SEO.import Link from "next/link";
import { Button } from "@chakra-ui/react";
function ChakraNextLinkButton({ href, children, ...props }) {
return (
<Link href={href} passHref>
<Button as="a" {...props}>
{children}
</Button>
</Link>
);
}
function IndexPage() {
return (
<ChakraNextLinkButton href="/about" colorScheme="facebook">
About
</ChakraNextLinkButton>
);
}
as
prop in chakra button on line number 7. We are rendering anchor tag instead of button on DOM and applying chakra button styles to it! You can pass all the chakra button props to the component ChakraNextLinkButton
like onClick, padding, margin etc. variant
prop to 'link'
to the same component to render the link styles.<ChakraNextLinkButton href="/about" variant="link">
About
</ChakraNextLinkButton>
Link
via props, you would need to separate the props in the custom component and pass it appropriately like belowimport Link from "next/link";
import { Button } from "@chakra-ui/react";
function ChakraNextLinkButton({ href, children, prefetch = true, ...props }) {
return (
<Link href={href} passHref prefetch={prefetch}>
<Button as="a" {...props}>
{children}
</Button>
</Link>
);
}
function IndexPage() {
return (
<ChakraNextLinkButton
href="/about"
colorScheme="facebook"
prefetch={false}>
About without prefetch
</ChakraNextLinkButton>
);
}
import Link from "next/link";
import { Link as ChakraLink } from "@chakra-ui/react";
function ChakraNextLink({ href, children, ...props }) {
return (
<Link href={href} passHref>
<ChakraLink {...props}>{children}</ChakraLink>
</Link>
);
}
function IndexPage() {
return (
<ChakraNextLink href="https://bharathikannan.com" isExternal color="red">
Visit my homepage
</ChakraNextLink>
)
}
isExternal
prop to the custom component, and that would forward the prop to Chakra's Link component to add the target="_blank"
and rel="noopener noreferrer"
attributes to rendered HTML automatically.import Link, { LinkProps } from "next/link";
import {
Button,
ButtonProps,
} from "@chakra-ui/react";
type ChakraAndNextProps = ButtonProps & LinkProps;
function ChakraNextLinkButton({
href,
children,
prefetch = true,
...props
}: ChakraAndNextProps) {
return (
<Link href={href} passHref prefetch={prefetch}>
<Button as="a" {...props}>
{children}
</Button>
</Link>
);
}
function IndexPage() {
return (
<ChakraNextLinkButton href="/about" colorScheme="facebook">
About
</ChakraNextLinkButton>
);
}
import Link, { LinkProps } from "next/link";
import {
Link as ChakraLink,
LinkProps as ChakraLinkProps,
} from "@chakra-ui/react";
type ChakraLinkAndNextProps = ChakraLinkProps & LinkProps;
function ChakraNextLink({ href, children, ...props }: ChakraLinkAndNextProps) {
return (
<Link href={href} passHref>
<ChakraLink {...props}>{children}</ChakraLink>
</Link>
);
}
function IndexPage() {
return (
<ChakraNextLink href="https://bharathikannan.com" isExternal color="red">
Visit my homepage
</ChakraNextLink>
);
}
Note: In the above examples, you are responsible for separating chakra props and next props and passing it to the corresponding components!
children
// BEFORE
function ChakraNextLinkButton({ href, children, prefetch = true, ...props }) {
return (
<Link href={href} passHref prefetch={prefetch}>
<Button as="a" {...props}>
{children}
</Button>
</Link>
);
}
// AFTER
function ChakraNextLinkButton({ href, prefetch = true, ...props }) {
return (
<Link href={href} passHref prefetch={prefetch}>
<Button as="a" {...props} />
</Link>
);
}
Do you think anything else to be added? Did you notice any issues in the post? Let me know at my Twitter handle, or please feel free to submit a PR to my post on the blog, How to use Nextjs Link with Chakra UI