1
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?

【備忘録】ReactRouteでパスパラメータidを受け取れるようにする

Posted at

やりたいこと

http://localhost:.../card/:idのパラメータidに設定された値を取得したい。

実装

pathで:idを設定、ReactRouterのuseParamsを使用しidを取得する。

App.tsx
return (
  <BrowserRouter>
  <Routes>
    <Route path="/" element={<Top />} />
     <Route path="/card/:id" element={<Card />} />
   </Routes>
 </BrowserRouter>
);
components/Card.tsx
import { useParams } from "react-router"

export function Card() {
  const {id} = useParams();
  return (
    <>
      <h1>{id}</h1>
    </>
  )
}

詰まっていたところ

パラメータの受け渡し方法がわからなかった

1
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
1
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?