0

I am using react-router-dom and am facing an issue with using <Routes>: Here is my index.tsx file:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

and this is my App.tsx file

import React from 'react';
import { RecoilRoot } from 'recoil';
import { BrowserRouter, BrowserRouter as Routes, Route } from 'react-router-dom';
import Loading from './sign/loading';
import SignIn from './sign/signIn';
import SignUp from './sign/signUp';
import SettingCare from './manage/settingCare';
import ManageCare from './manage/manageCare';
import AddCare from './manage/addCare';
import Main from './main/main';

function App() {
  return (
    <RecoilRoot>
      <Routes>
        <Route path='/' element={<Main />} />
        <Route path='/loading/*' element={<Loading />} />
        <Route path='/sign-in/*' element={<SignIn />} />
        <Route path='/sign-up/*' element={<SignUp />} />
        <Route path='/setting-care/*' element={<SettingCare />} />
        <Route path='/manage-care/*' element={<ManageCare />} />
        <Route path='/add-care/*' element={<AddCare />} />
      </Routes>
    </RecoilRoot>
  );
}

export default App;

With this code, I am getting the following error: enter image description here

After searching, I added the <BrowserRouter> element, like so:

...
function App() {
  return (
    <RecoilRoot>
      <BrowserRouter>
        <Routes>
          <Route path='/' element={<Main />} />
          <Route path='/loading/*' element={<Loading />} />
          <Route path='/sign-in/*' element={<SignIn />} />
          <Route path='/sign-up/*' element={<SignUp />} />
          <Route path='/setting-care/*' element={<SettingCare />} />
          <Route path='/manage-care/*' element={<ManageCare />} />
          <Route path='/add-care/*' element={<AddCare />} />
        </Routes>
      </BrowserRouter>
    </RecoilRoot>
  );
}
...

But, it still doesn't work, giving the following error:

enter image description here

2
  • .tsx file in a react project? Commented Jan 10, 2022 at 4:05
  • yes, it is problem of file import. react-router-dom v6 is really confuse to me;;; Commented Jan 10, 2022 at 4:08

3 Answers 3

5

This import

import { BrowserRouter, BrowserRouter as Routes, Route } from 'react-router-dom';

aliases BrowserRouter to two different things.

Change the import to

import { BrowserRouter, Routes, Route } from 'react-router-dom';

and the error should go away.

Sign up to request clarification or add additional context in comments.

Comments

1

Routes is a separate tag that wraps Route tag and cannot be imported and assigned to BrowerRouter. So import them separately. Like So:

import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

Comments

0

SOLUTION: your Import please change it to as below

import { BrowserRouter, BrowserRouter as Routes, Route } from 'react-router-dom';

Change your import to

import { BrowserRouter as Routes, Route } from 'react-router-dom';

ERROR GONE!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.