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?

【${id} と :id の違い】

Posted at

${id} は JavaScriptのテンプレートリテラル の構文で、変数を埋め込むために使う。

const id = 5;
const url = `/PickupThread/${id}`; // 結果: "/PickupThread/5"
navigate(url); // id を含めて画面遷移

つまり、${id} を使うと、実際の投稿ID(例えば 5)が入ったURL に展開される!

一方、:idは React Router のルート(パスパラメータ) の定義として使います。 ルートを設定する際に、動的なパスとして IDが変わる部分を :id で指定 します。

<Route path="/PickupThread/:id" element={<PickupThread />} />

この書き方で、React Router に「idの部分は任意の数値が入るよ」と伝えている! そのため、実際のURLが/PickupThread/3/PickupThread/10になったとき、useParams()を使って:idの値を取得 できる

const { id } = useParams(); // URLの :id 

の部分を取得

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?