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

useHref error with react router v7 #147

Open
rattermeyer opened this issue Jan 9, 2025 · 6 comments
Open

useHref error with react router v7 #147

rattermeyer opened this issue Jan 9, 2025 · 6 comments

Comments

@rattermeyer
Copy link

I am currently migrating an app from remix 2 to react router v7.

I have, unfortunately only sometimes, at least in dev mode, .... I get the error:

useHref() may be used only in the context of a component.

Using

  • remix-hook-form 6.1.2
  • react-router 7.1.1

The following route / component:

export default function HrefTest({ loaderData }: Route.ComponentProps) {
    const basename = useHref('/');
    const form = useRemixForm<FormData>({resolver});
    return (
        <div>
            <h1>Test</h1>
        </div>
    );
}

throws the error in useRemixForm not the line above.

@rattermeyer
Copy link
Author

As a help, here the component hierarchy:

image

@markoremus
Copy link

I've the exact same issue - after migrating from remix 2 to rr7 my app is crashing with the following error:

useHref() may be used only in the context of a component.

I'm using the following versions:

  • remix-hook-form 6.1.2
  • react-router 7.1.1
  • vite 6.0.7

@sheepsteak
Copy link

sheepsteak commented Jan 14, 2025

It seems that this is an upstream issue in React Router - remix-run/react-router#12475.

I've managed to fix it for myself in the time being by removing all the module-sync declarations in the exports of each React Router package I'm using:

https://github.com/remix-run/react-router/blob/b4a37a8be273e88e3f6469e64865ca25416a16ce/packages/react-router/package.json#L28

You could use something like patch-package for now to remove them until React Router push a fix. Although, as I'm writing this I just saw they've merged a fix (remix-run/react-router#12729) and it's available in 7.1.2-pre.0. Will try it out and report back.

EDIT: React Router 7.1.2-pre.0 seems to fix it for me 🎉

@markoremus
Copy link

@sheepsteak: thanks for your feedback! I can confirm using the react router pre-release (7.1.2-pre.0) seems to have fixed the issue.

@AlemTuzlak
Copy link
Contributor

This indeed is an issue with react-router that they have been working on for a few weeks now, if it wasn't for useHref it would complain about useSubmit as it doesn't properly de-dupe dependencies.

@rattermeyer
Copy link
Author

Thanks a lot for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants