import { BrowserRouter, Routes, Route } from "react-router-dom";
//Layouts
import HomeLayoutRoute from "./components/layouts/HomeLayout";
//components
import Home from './components/Home';
//import Dashboard from './components/Dash';
// Routing
import PrivateRoute from "./components/routing/PrivateRoute";
// Screens
import PrivateScreen from "./components/loginscreens/PrivateScreen";
import LoginScreen from "./components/loginscreens/LoginScreen";
import RegisterScreen from "./components/loginscreens/RegisterScreen";
import ForgotPasswordScreen from "./components/loginscreens/ForgotPasswordScreen";
import ResetPasswordScreen from "./components/loginscreens/ResetPasswordScreen";
const App = () => {
return (
<BrowserRouter>
<div className="app">
<Routes>
<HomeLayoutRoute path="/" element={<Home />} />
<PrivateRoute path="/" element={<PrivateScreen/>} />
<Route path="/login" element={<LoginScreen/>} />
<Route path="/register" element={<RegisterScreen/>} />
<Route path="/forgotpassword" element={<ForgotPasswordScreen/>}/>
<Route path="/passwordreset/:resetToken" element={<ResetPasswordScreen/>}/>
</Routes>
</div>
</BrowserRouter>
);
};
export default App;
This is my App.js file This is the Error : Error: You cannot render a inside another . You should never have more than one in your app.
This code works with React-Router-Dom Version 5, But When I move to React-Router-Dom version 6 this error came.
CodePudding user response:
I'm using react-router-dom
version 5.3.0 and I can't find an exported member named 'Routes'. Not sure if this member comes from an older version of react-router-dom
.
That said, I think you might want to replace <Routes>
with <Switch>
.
<Switch>
renders the first child <Route>
or <Redirect>
that matches the location.
<BrowserRouter>
<div className="app">
<Switch>
<HomeLayoutRoute path="/" element={<Home />} />
<PrivateRoute path="/" element={<PrivateScreen/>} />
<Route path="/login" element={<LoginScreen/>} />
<Route path="/register" element={<RegisterScreen/>} />
<Route path="/forgotpassword" element={<ForgotPasswordScreen/>}/>
<Route path="/passwordreset/:resetToken" element={<ResetPasswordScreen/>}/>
</Switch>
</div>
</BrowserRouter>
(https://reactrouter.com/web/api/Switch)
Edit:
As for the error: "You cannot render a inside another . You should never have more than one in your app" -> I think it might be related to the problem I mentioned above but it can also be because you have a duplicated router inside another. (E.g. the component <ForgotPasswordScreen/>
contains another <Route>
element inside).
CodePudding user response:
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
//Layouts
import HomeLayoutRoute from "./components/layouts/HomeLayout";
//components
import Home from './components/Home';
//import Dash from './components/DashBoard';
// Routing
import PrivateRoute from "./components/routing/PrivateRoute";
// Screens
import PrivateScreen from "./components/loginscreens/PrivateScreen";
import LoginScreen from "./components/loginscreens/LoginScreen";
import RegisterScreen from "./components/loginscreens/RegisterScreen";
import ForgotPasswordScreen from "./components/loginscreens/ForgotPasswordScreen";
import ResetPasswordScreen from "./components/loginscreens/ResetPasswordScreen";
const App = () => {
return (
<Router>
<div className="app">
<Routes>
<HomeLayoutRoute exact path="/" component={Home} />
<PrivateRoute exact path="/" component={PrivateScreen} />
<Route exact path="/login" component={LoginScreen} />
<Route exact path="/register" component={RegisterScreen} />
<Route
exact
path="/forgotpassword"
component={ForgotPasswordScreen}
/>
<Route
exact
path="/passwordreset/:resetToken"
component={ResetPasswordScreen}
/>
</Routes>
</div>
</Router>
);
};
export default App;
This is the Code I have used with React-Router-Dom Version 5 I think problem with the Version 6