28
loading...
This website collects cookies to deliver better user experience
npm install [email protected]
npm install react-router-dom
npm install react-location
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import {BrowserRouter as Router} from "react-router-dom"
import reportWebVitals from "./reportWebVitals";
ReactDOM.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>,
document.getElementById("root")
);
import {ReactLocation} from "react-location"
import AboutPage from "./pages/About"
import HomePage from "./pages/Home"
// create a location object
export const location = new ReactLocation()
// create a routes object
export const routes = [
{ path: "/", element: <HomePage /> },
{ path: "/about", element: <AboutPage/>}
]
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { Router } from "react-location";
import { location, routes } from "./location";
ReactDOM.render(
<Router location={location} routes={routes}>
<React.StrictMode>
<App />
</React.StrictMode>
</Router>,
document.getElementById("root")
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
<Switch>
<Route path="/about" component={About}/>
</Swich>
<Switch>
<Route path="/about">
<About/>
</Route>
</Swich>
<Switch>
<Route path="/about" render={(routerProps) => <About {...routerProps}/>}/>
</Swich>
<Routes>
<Route path="/about" element={<About/>}>
<Routes>
// create a routes object
export const routes = [
{ path: "/", element: <HomePage /> },
{ path: "/about", element: <AboutPage/>}
]
import {Outlet} from "react-location"
function App() {
return (
<div className="App">
<Outlet/>
</div>
);
}
export default App;
/about/:person
which gets saved in a variable of the same name. How to access the param can differ.const person = props.match.params.person
import {useParams} from "react-router-dom"
const params = useParams()
const person = params.person
import {useMatch} from "react-location"
const match = useMatch()
const params = match.params
const person = params.person
<Link>
component to link from one route to another, but what happens when you want to redirect from inside a function?props.history.push("/")
import {useNavigate} from "..."
const navigate = useNavigate()
navigate("/")