解決済み: 反応して現在のルートを取得

反応して現在のルートを取得する際の主な問題は、遅くなる可能性があることです。

There is no built-in way to get the current route in React. However, you can use the router's context to get the current location, which will give you the current pathname.

const { location } = this.context.router; console.log(location.pathname); // /some/path

uselocation() フック

uselocation() フックは、コンポーネントをルートでレンダリングするかどうかを決定するために使用されます。 コンポーネントがルートでレンダリングされるべきでない場合は、このフックを使用して、代わりにレンダリングする場所を決定できます。

ルートを取得する方法

React でルートを取得する方法はいくつかあります。 XNUMX つの方法は、react-router を使用することです。 別の方法は、react-router-dom を使用することです。

関連記事:

コメント