Answers for "get current route in react js"

3

react get current route

/*In the 5.1 release of react-router there is a hook called useLocation,
which returns the current location object. 
This might be useful any time you need to know the current URL.*/
import { useLocation } from 'react-router-dom';

const location = useLocation();
console.log(location.pathname);
Posted by: Guest on December-24-2020
0

get current url react router

import {withRouter} from 'react-router-dom';

const SomeComponent = withRouter(props => <MyComponent {...props}/>);

class MyComponent extends React.Component {
  SomeMethod () {
    const {pathname} = this.props.location;
  }
}
Posted by: Guest on May-21-2020
0

how to know the current route in react class component

/*For class based components*/
import { withRouter } from 'react-router-dom';

class SomeComponent extends Component {
  render() {
    console.log(this.props.location)
    console.log(this.props.location.pathname)
    console.log(this.props.location.match)
    return <div>Something</div>
  }
}

export default withRouter(SomeComponent)
Posted by: Guest on August-09-2021

Code answers related to "get current route in react js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language