Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Link component doesn't seem to forward refs #299

Closed
alcar opened this issue May 19, 2023 · 1 comment · Fixed by #300
Closed

The Link component doesn't seem to forward refs #299

alcar opened this issue May 19, 2023 · 1 comment · Fixed by #300
Labels
bug Something isn't working unconfirmed Needs triage.

Comments

@alcar
Copy link

alcar commented May 19, 2023

Description

Passing a ref prop to the Link component causes the following error:

Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Mandatory reproduction URL (CodeSandbox or GitHub repository)

https://github.com/alcar/next-intl-ref-issue

Reproduction description

Steps to reproduce:

  1. Install the dependencies with npm i.
  2. Run the app with npm run dev.
  3. Visit the app (defaults to http://localhost:3000).
  4. Open the dev tools.
  5. See the error in the console.

Expected behaviour

I would expect the ref to be passed all the way down to Next.js's Link.

Since BaseLink already contains a forwardRef, I believe adding a forwardRef to Link would suffice. I've tried the following code locally (npm link) and it seems to fix the issue.

// ...

function Link({locale, ...rest}: Props, ref: Ref<HTMLAnchorElement>) {
  const defaultLocale = useClientLocale();

  return <BaseLink ref={ref} locale={locale || defaultLocale} {...rest} />;
}

export default forwardRef(Link);

I'm happy to open a PR if that solution looks good!

@alcar alcar added bug Something isn't working unconfirmed Needs triage. labels May 19, 2023
@amannn
Copy link
Owner

amannn commented May 22, 2023

Great point, thanks for the detailed bug report and the analysis! I've just added #300 to address this and will cut a patch release once CI passes. I'll also publish a new RSC beta release on top of this, in case you use that.

I'm happy to open a PR if that solution looks good!

Sorry, I saw this a bit too late! In case you want to contribute in the future you're more than welcome to! 🙂

amannn added a commit that referenced this issue May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed Needs triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants