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

useHistoryを使うと画面が真っ白になった

2
Posted at

はじめに

useHistoryは廃止されていました。

問題

useHistoryを使ったら画面が真っ白になりエラーになった。

import { useHistory } from "react-router-dom";
export const Top = () => {
  const history = useHistory();
  const onClickAdmin = () => {
    history.push("/users");
  };
};

解決方法

useHistoryはreact-router-dom v6で廃止されました。
v6ではuseNavigateに置き換わっています。

import { useNavigate } from "react-router-dom";
export const Top = () => {
  const navigate = useNavigate();
  const onClickAdmin = () => {
    navigate("/users");
  };
};

おわりに

react-router-dom v6ではuseHistoryが廃止され、useNavigate に統一されました。
v5から移行する際はご注意ください。同じエラーで詰まった方の参考になれば幸いです。

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