[英]Property 'push' does not exist on type 'NavigateFunction' Typescript
As new Version 6 doesn't allow useHistory;由于新版本 6 不允许使用历史记录; I used "useNavigate" instead of useHistory.我使用“useNavigate”而不是 useHistory。 After taking user input, those input data is supposed to go next page.在接受用户输入后,这些输入数据应该是 go 下一页。 While using "History.push" command to pass the data it's showing this error:使用“History.push”命令传递数据时显示此错误:
Property 'push' does not exist on type 'NavigateFunction' “NavigateFunction”类型上不存在属性“push”
Here is the code:这是代码:
import { useNavigate } from "react-router-dom";
export interface UserData {
name: string;
gender: string;
lang: string;
}
const history = useNavigate();
const [UserData, setUserData] = useState<UserData>({
name: "",
gender: "Male",
lang: "React",
});
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setUserData({ ...UserData, [name]: value });
};
const handleSubmit = () => {
history.push("/exam", UserData);
};
what am I supposed to change and add?我应该更改和添加什么?
The useNavigate
hook returns a function , not a history object with a push
method. useNavigate
钩子返回一个function ,而不是带有push
方法的历史记录 object 。
declare function useNavigate(): NavigateFunction; interface NavigateFunction { ( to: To, options?: { replace?: boolean; state?: any } ): void; (delta: number): void; }
Rename history
to navigate
so there's no future confusion.重命名history
以navigate
,以免将来出现混淆。
const navigate = useNavigate();
Invoke the navigate
function and pass the state in the second argument, the optional options
parameter, under the state
property.调用navigate
function 并在第二个参数(可选options
参数)中传递 state 属性下的state
。
const handleSubmit = () => {
navigate("/exam", { state: UserData });
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.