“UseroutEmatch” Kod odpowiedzi

React Router DOM Hook ścieżki

import { useLocation } from 'react-router-dom'

// Location is, for example: http://localhost:3000/users/new

// Care! MyComponent must be inside Router to work
const MyComponent = () => {
	const location = useLocation()
    
    // location.pathname is '/users/new'
    return <span>Path is: {location.pathname}</span>
}

export default MyComponent
Mr Dom

UseroutEmatch

// React Router v5:
// hook without argument returns match obj of current <Route>
// or takes obj argument identical to 'props' argument of matchPath
import { useRouteMatch } from 'react-router-dom'

function BlogPost() {
 let match = useRouteMatch({ 
   '/blog/:slug', 
   strict: true, 
   sensitive: true 
 })
 // Do something with the match...
 return <div />
}
Coffee Addict

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

Przeglądaj inne języki kodu