我目前正在使用 React 和 TypeScript。這是我的問題:當我嘗試執行 history.push 時,它回傳如下錯誤: Uncaught (in promise) TypeError: history.push is not a function
這是我的代碼:
interface Props extends
WithTranslation {
history: any;
}
const Menu: React.FC<Props> = ({
history
}) => {
if (await MarketplaceRequests.sessionsTest()) {
history.push({
pathname: '/',
});
}
return ......
先感謝您
uj5u.com熱心網友回復:
Idk 如何配置您的路線。但是您是否嘗試過從鉤子中獲取歷史并洗掉從道具中獲取的方式
const history = useHistory();
uj5u.com熱心網友回復:
我認為在 react-router-dom v6 useHistory 不起作用或已棄用。而是使用useNavigate:
import {useNavigate} from 'react-router-dom'
const navigate = useNavigate()
const Menu: React.FC<Props> = ({
history
}) => {
if (await MarketplaceRequests.sessionsTest()) {
navigate('/');
}
uj5u.com熱心網友回復:
如果你使用 React Router Dom > v6,你需要使用 useNavigate。
import { useNavigate } from 'react-router-dom';
const navigate = useNavigate();
navigate('/route');
但是,如果沒有,您需要useHistory像上面示例中的 useNavigate 一樣匯入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/449552.html
