React Router Dom

SetUp (with Redux)

import React from "react";
import ReactDOM from "react-dom";
import * as serviceWorker from "./serviceWorker";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Dashboard from "./pages/dashboard";
import Index from "./pages";
import { Provider } from "react-redux";
import RoomApp from "./reducer";
import { createStore } from "redux";
const store: any = createStore(RoomApp, {});

ReactDOM.render(
<Provider store={store}>
  <Router>
    <div>
      <Switch>    
        <Route path="/dashboard" component={Dashboard} />
        <Route path="/" component={Index} />
      </Switch>
    </div>
   </Router>
  </Provider>,
  document.getElementById("root")
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();

History and Location

Pure JS

  • for the navigating to new page and push it into history stack

  • we can obtain different info from window.location

Library

  • for navigation, we can simply use useNavigate and Link

Matching

Setting Routes

Routes Ranking

  • On Main or Halo component, can write something like this, as the path is inherited, no need to enter the full relative path

Last updated

Was this helpful?