gpt4 book ai didi

javascript - (0 , _reactRouterDom.useHistory) 不是函数

转载 作者:行者123 更新时间:2023-12-05 00:28:16 26 4
gpt4 key购买 nike

我正在使用 react-router-dom v6.0.0-beta.0 和 reac-redux 7.2.2 并想通过使用 useState 钩子(Hook)来编辑状态值,但是当我单击 EditIcon 时,它给了我这个错误......

(0 , _reactRouterDom.useHistory) is not a function


EditTodo.js
import React, { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useHistory } from "react-router-dom";
import { useParams } from "react-router-dom";

import { todoUpdated } from "./TodoSlice";

export const EditTodo = () => {
const { todoId } = useParams();

const todo = useSelector((state) =>
state.todo.find((todo) => todo.id === todoId)
);

const [name, setName] = useState();
const [description, setDescription] = useState();

const dispatch = useDispatch();
const history = useHistory();

const onTitleChanged = (e) => setName(e.target.value);
const onContentChanged = (e) => setDescription(e.target.value);

const onSavePostClicked = () => {
if (name && description) {
dispatch(todoUpdated({ id: todoId, name, description }));
history.push(`/todo/${todoId}`);
}
};

return todo ? (
<section>
<h2>Edit Post</h2>
<form>
<label htmlFor="postTitle">Post Title:</label>
<input
type="text"
id="postTitle"
name="postTitle"
placeholder="What's on your mind?"
value={name}
onChange={onTitleChanged}
/>
<label htmlFor="postContent">Content:</label>
<textarea
id="postContent"
name="postContent"
value={description}
onChange={onContentChanged}
/>
</form>
<button type="button" onClick={onSavePostClicked}>
Save Post
</button>
</section>
) : null;
};

如何解决这个问题?如果有人知道帮助我解决这个问题。谢谢
这是 codesandbox

最佳答案

您正在使用 react-router-dom 的 beta 版本。在 v6 中,useHistory替换为 useNavigate https://dev.to/zenveus/routing-with-react-router-v6-6b1
如果你想使用 useHistory,你应该安装 v5。

const navigate = useNavigate();

const onSavePostClicked = () => {
if (name && description) {
dispatch(todoUpdated({ id: todoId, name, description }));
navigate(`/todo/${todoId}`);
}
};

关于javascript - (0 , _reactRouterDom.useHistory) 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66166264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com