skip to Main Content
Hello, I am new to React ,this problem acccured and never found the solution or expalanation
import { BrowserRouter, Routes, Route } from "react-router-dom";
// import Product from "./pages/Product";
import Home from "./pages/Home";

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="Home" element={<Home />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

the error was : No routes matched location "/" . and this is the comopnent tree : [components tree]your text`(https://phpout.com/wp-content/uploads/2023/11/eBKUb.png)

I coudn’t find an explanation`

2

Answers


  1. Change the path of your root route inside browser router to “/“.

    Right now you have a single route defined, which matches only the url-path “home“.

    Login or Signup to reply.
  2. the root URL ("/") is often the starting point and the default route.
    However if you don’t want to have a ("/") route in your application you can use Redirect to redirect to /home from /

    import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
    
    function App() {
      return (
        <Router>
          <Switch>
            <Route exact path="/" render={() => <Redirect to="/home" />} />
            <Route path="/home" component={Home} />
          </Switch>
        </Router>
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search