はじめに
個人開発メモ📝
react-routerを使用したく、useHistoryを書いたらエラーになった話。
useHistoryを使用してみる
npm install react-router-dom
Register.jsx
import { useHistory } from 'react-router-dom';
function RegistrationForm() {
const history = useHistory();
// 省略
.then(data => {
history.push('/login');
})
このように書いたら、
Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/react-router-dom.js?v=2f8e3fa7' does not provide an export named 'useHistory'
このエラーメッセージがローカル上に出ました。
対処法
react-router-dom バージョン6 からは、useNavigateが推奨になったとのこと。