I am using the latest version of react router. When I am using routes in my component, They are not rendering anything but When I remove the routes and use simply the component they are working fine. Not able to understand what is going wrong
This do not work and is not rendering anything on “/” or http://localhost:3000/
JavaScript
x
18
18
1
import React from "react";
2
import { BrowserRouter as Router, Route, Navigate } from "react-router-dom";
3
import Users from "./user/pages/Users";
4
5
function App() {
6
return (
7
<Router>
8
<Route path="/" exact>
9
<Users />
10
</Route>
11
<Navigate to="/" />
12
</Router>
13
);
14
}
15
16
export default App;
17
18
This is rendering and working fine.
JavaScript
1
11
11
1
import React from "react";
2
import { BrowserRouter as Router, Route, Navigate } from "react-router-dom";
3
import Users from "./user/pages/Users";
4
5
function App() {
6
return <Users />;
7
}
8
9
export default App;
10
11
Advertisement
Answer
JavaScript
1
19
19
1
import React, {useState} from "react";
2
import { BrowserRouter as Router, Routes, Route, Navigate } from "react-router-dom";
3
import Users from "./user/pages/Users";
4
import Profiles from "./Profiles" // this is dummy
5
6
function App() {
7
const [state, setState] = useState(false)
8
return (
9
<Router>
10
<Routes>
11
<Route path="/" element={<Users />}/>
12
<Route path="/profiles" element={state ? <Profiles /> : <Navigate to="/" />} />
13
{/* so you redirect only if your state is false */}
14
</Routes>
15
</Router>
16
);
17
}
18
19
export default App;