react-router-dom: getting props.location from within component

前端 未结 4 1254
野的像风
野的像风 2021-02-20 14:03

I have a simple App that uses BrowserRouter from \'react-router-dom\' v4. I\'m trying to access the location.pathname property from within the &l

4条回答
  •  长情又很酷
    2021-02-20 14:24

    Since react-router v5.1.0 you can use useLocation.

    https://reactrouter.com/web/api/Hooks/uselocation

    class App extends Component{
      render(){
        const location = useLocation();
    
        return (
          
    //...
    ); } } // ...

提交回复
热议问题