简体   繁体   中英

Uncaught Error: useNavigate() may be used only in the context of a <Router> component [react-router-dom 6.4]

For the last couple of hours, I'm trying to figure this out, but no success. This works perfectly fine with react-router-dom 6.3, but as soon as I upgrade to any 6.4 version, I get mentioned error. I already checked, and all my imports are from 'react-router-dom',

The Orders component does contain useNavigate(), but as you can see, HashRouter is the highest it can be. This is my simplified index.tsx and it still does not work.

const container = document.getElementById('root');
if (container) {
  const root = createRoot(container);
  root.render(
    <React.StrictMode>
      <HashRouter>
        <Provider store={store}>
          <ChakraProvider theme={theme}>
            <Routes>
              <Route path='/' element={<Orders />} />
            </Routes>
          </ChakraProvider>
        </Provider>
      </HashRouter>
    </React.StrictMode>,
  );
}

Any ideas?

No idea why, but switching to ViteJs from CRA fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM