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-route-domのusenavigateの使い方

Posted at

usehistoryは主にページ遷移をする際に使われます。
そして、React-route-domのv6からは、usehistoryからusenavigateを使用します。
自分自身v6なのに、間違ってusehistoryを使用してエラーが出てしまうといったことがあるので、使い方を説明します。

usehistoryの場合

import {useHistory} from "react-router-dom"

const history = useHistory();
history.push("/");
history.goBack();

usenavigateの場合


import {useNavigate} from "react-router-dom"

const navigate = useNavigate();
navigate("/");
navigate(-1);

基本的には、使い方は同じで、違いとしては、usenavigateだとpushを使用しないでも使えます。
1つ前に戻る時の書き方は若干異なります。

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?