“Jak poznać bieżącą trasę w komponencie klas react” Kod odpowiedzi

React Uzyskaj bieżącą trasę

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

Jak poznać bieżącą trasę w komponencie klas 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

Odpowiedzi podobne do “Jak poznać bieżącą trasę w komponencie klas react”

Pytania podobne do “Jak poznać bieżącą trasę w komponencie klas react”

Więcej pokrewnych odpowiedzi na “Jak poznać bieżącą trasę w komponencie klas react” w JavaScript

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu