0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[React, Next.js]URLパラメータを取得

Posted at

はじめに

今回はReactとNext.jsでURLの情報を取得する方法を書いていきます。

Reactの場合

ReactでURLのパラメータを取得するには、「useLocation()」が使えます。
まずは、「react-router-dom」をインストールしてください。

npm install react-router-dom

以下がサンプルコードになります。
「localhost://3000/Favorite」の場合

const location = useLocation();
console.log(location);

結果☟
スクリーンショット 2023-11-03 20.29.41.png

Next.jsの場合

Next.jsでURLのパラメータを取得するには、useRouter()が使えます。
以下がサンプルコードになります。
「localhost://3000/books」の場合

const router = useRouter();
console.log(router);

結果☟
スクリーンショット 2023-11-03 20.36.53.png

参考

参考にしたドキュメントです。

最後に

他にも色々な記事を書いているので、よければ読んでいってください、、、

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?