react-notion-x icon indicating copy to clipboard operation
react-notion-x copied to clipboard

Avoid page reload when clicking on links

Open junogueira opened this issue 2 years ago • 3 comments
trafficstars

Description

Is there a way to avoid a page reload when clicking on links?

I'm using the full example and apparently it's the default behavior. But when I included a few <Link> directly on my page it seems to work as expected.

I'm new to Next.js / React so I might be missing something basic here.

Thanks.

junogueira avatar Jan 31 '23 11:01 junogueira

Are you able to post your repo, and link to your NotionRenderer component with that code? I am having the same issue with Images, where nextImage isn't taking effect, despite <Image> working fine directly on the page. #447

bmitchinson avatar Mar 05 '23 18:03 bmitchinson

I was using the full example as it is, no changes.

junogueira avatar Mar 07 '23 14:03 junogueira

Slightly hacky but I found this works:

import type { AProps } from "react-html-props";
import Link from "next/link";

const PageLink = ({ href, children, ...props }: AProps) => {
  return (
    <Link href={href}>
      <a {...props}>{children}</a>
    </Link>
  );
};

const components = React.useMemo(
  () => ({
    PageLink: PageLink,
  }),
  []
);

The key thing to note is we are passing in PageLink into components, not nextLink. Digging through the source code I found that the library uses PageLink to render links.

Note that I tested this on Next 12 only. Next 13 changed how Link works and I have not yet tested if it works on there or not.

I'm not sure if this has any unintended side effects but so far it seems to be working fine for me.

Armster15 avatar May 12 '23 07:05 Armster15