useparams string code example

Example 1: useparams

import {
  BrowserRouter as Router,
  Switch,
  Route,
  useParams
} from "react-router-dom";

function BlogPost() {
  let { slug } = useParams();
  return <div>Now showing post {slug}</div>;
}

ReactDOM.render(
  <Router>
    <Switch>
      <Route exact path="/">
        <HomePage />
      </Route>
      <Route path="/blog/:slug">
        <BlogPost />
      </Route>
    </Switch>
  </Router>,
  node
);

Example 2: useparams

import { useParams } from "@reach/router"

// route: /user/:userName
const User = () => {
  const params = useParams();

  return <h1>{params.userName}</h1>
)