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?

【React】react-router-dom にて画面を更新・リセットする方法

Posted at

問題

react-router-domの navigate の画面遷移を用いて、今開いている画面を更新したい。

方法

navigate の引数を"0"にする。

Page.tsx
import { useNavigate } from "react-router-dom";

const Page = () => {
  const navigate = useNavigate();

  const refreshPage = () => {
    navigate(0);// 画面更新
  };

  // 省略
};

これで、画面更新をすることができます。

また、"1" を指定すると、一つ先に進み、"-1"を指定すると、一つ前に戻すこともできます。

Page.tsx
import { useNavigate } from "react-router-dom";

const Page = () => {
  const navigate = useNavigate();

  const goBack = () => {
    navigate(-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?