2
4

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 3 years have passed since last update.

Reactのページ遷移でstateを渡す

Posted at

Reactでページ遷移の際に一緒にstateを渡す方法をいつも忘れてしまうので、書き残します。

stateを渡す側

Link

react-router-domのLinkを使う場合、

link.ts
<Link to={{ pathname: '/path', state: {hoge: hoge} }}>
  <Button/>
</Link>

history push

history.push()の場合、

history.ts
onClick={() => { history.push('/path', {hoge: hoge}) }}

もらう側

to.ts
const location = useLocation();
const state = location.state;

以上、こんな感じでやりとりできます!

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?