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

[BUG] - React Browser Router - Link always reload whole page #4897

Closed
komenixx opened this issue Feb 21, 2025 · 6 comments
Closed

[BUG] - React Browser Router - Link always reload whole page #4897

komenixx opened this issue Feb 21, 2025 · 6 comments

Comments

@komenixx
Copy link

HeroUI Version

2.7.2

Describe the bug

useNavigate has no effect on how Link component handle routing behavior.

App.tsx

import {Suspense, lazy} from 'react'
import {Navigate, NavigateOptions, Route, Routes, useNavigate} from 'react-router-dom'
import {HeroUIProvider} from '@heroui/react'
import Suspenser from '@ui/suspenser.tsx'

declare module '@react-types/shared' {
  interface RouterConfig {
    routerOptions: NavigateOptions
  }
}

function App() {
  const navigate = useNavigate()

  return (
      <HeroUIProvider navigate={navigate}>
        <Suspense fallback={<Suspenser />}>
          <Routes>
              <Route element={<SignInPage />} path="/auth" />
              <Route element={<SignInPage />} path="/auth/sign-in" />
              <Route element={<SignOutPage />} path="/auth/sign-out" />
              <Route element={<ForgotPasswordPage />} path="/auth/forgot-password"/>
              <Route element={<SetNewPasswordPage />} path="/auth/set-new-password"/>
              <Route element={<NotFoundPage />} path="*" />
          </Routes>
        </Suspense>
      </HeroUIProvider>
  )
}

Component.tsx

import React, {useContext, useEffect} from 'react'
import {Button, Link, Spacer} from '@heroui/react'

export default function Comp() {

  return <Button as={Link} href={'/auth/sign-in'} variant="light">
    Sign In
  </Button>
}

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

  1. Set up Browser Router module from react-router-dom
  2. Click on Link or Button with as Link attribute
  3. Whole page will reload

Expected behavior

I expect browser routing behavior without reload whole page.

Screenshots or Videos

No response

Operating System Version

macOS

Browser

Chrome

Copy link

linear bot commented Feb 21, 2025

@iSaulX
Copy link

iSaulX commented Feb 21, 2025

Hey there! This is not a bug. There's two ways to fix this.
The first one is just importing Link from react-router instead of using Link from HeroUI. Something like this:

import {Link} from "react-router-dom"; 

export default function YourButton(){
    return (
           <Button as={Link} to="/yout/path">Button</Button>
   )
}

And the other one it's using useNavigate and useHref from React Router and setting up in the Global Provider.

import type {NavigateOptions} from "react-router-dom";

import {BrowserRouter, useNavigate, useHref} from "react-router-dom";
import {HeroUIProvider} from "@heroui/react";

declare module "@react-types/shared" {
  interface RouterConfig {
    routerOptions: NavigateOptions;
  }
}

function App() {
  const navigate = useNavigate();

  return (
    <HeroUIProvider navigate={navigate} useHref={useHref}>
      {/* Your app here... */}
      <Routes>
        <Route path="/" element={<HomePage />} />
        {/* ... */}
      </Routes>
    </HeroUIProvider>
  );
}

PD: I think you're missing adding useHref of your global provider.

Take a look to the docs setting up React Router with HeroUI. documentation

@wingkwong
Copy link
Member

@komenixx Please try to add useHref to your provider and make sure you are using the latest versions. If it still fails, please provide a sandbox for us to investigate. Thanks.

@komenixx
Copy link
Author

@wingkwong adding useHref had no effect.

I simply override default HeroUI. Working solution is using Link from react-router-dom, by @iSaulX

@AhmedLukman
Copy link

AhmedLukman commented Feb 26, 2025

Same issue in Next.js regarding full page reloads. Here is a summary: in providers.tsx:

"use client";

import { useRouter } from "next/navigation";
import { HeroUIProvider } from "@heroui/system";

export function Providers({ children }: { children: React.ReactNode }) {
  const router = useRouter();

  return <HeroUIProvider navigate={router.push}>{children}</HeroUIProvider>;
}

then when using in component:

import { Link } from "@heroui/link";
import { Button } from "@heroui/button";

<Button as={Link} href="existing-route-in-the-site">Test</Button>

@AhmedLukman
Copy link

Works now, update everything of heroui to latest

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