Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a variable from the path in react router?

My react router works fine.. but I would like get the date from the Path as the title which expects a string.

  <Route exact path="/meeting/:date"
              breadcrumb="Meetings"
              title={DATE}
              component={(props) => (
                <FooComponent
                  date={props.match.params.date} />
              )}
            />

thanks

like image 876
Embet Isit Avatar asked Jan 03 '18 20:01

Embet Isit


People also ask

How do I get the path variable in React router?

To get path params in React Router, we can use the useParams hook. We create the Child component that calls the useParams hook to return an object with the route params in the URL. And we render the value of the id param on the page.


1 Answers

There is a new api in react-router-dom 5.1

https://github.com/ReactTraining/react-router/releases/tag/v5.1.0

import {useParams} from "react-router-dom";

function MyComponent(){
    const {date} = useParams()
}

like image 50
Ihsan Müjdeci Avatar answered Oct 14 '22 04:10

Ihsan Müjdeci