“React Obtenha a rota atual” Respostas de código

React Obtenha a rota atual

/*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);

Snackety Snack

Obtenha o roteador atual de URL React

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

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

class MyComponent extends React.Component {
  SomeMethod () {
    const {pathname} = this.props.location;
  }
}
Jolly Jaguar

Como saber a rota atual no componente da classe React

/*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)
Frightened Flatworm

Respostas semelhantes a “React Obtenha a rota atual”

Perguntas semelhantes a “React Obtenha a rota atual”

Mais respostas relacionadas para “React Obtenha a rota atual” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código