当按下主页按钮时,我想将其重定向到主页“/”。但我收到这个错误。我能做些什么 ? :
import { Route, useHistory } from 'react-router-dom/cjs/react-router-dom.min';
const history = useHistory();
const homeButtonOnClickHandler = () =>{
history.push("/");
}
<Router>
<Header homeButtonOnClickHandler={homeButtonOnClickHandler}/>
<Switch>
<Route path='/result'>
<Result
ingredients={ingredients}
total={() => hesapla()}
/>
</Route>
<Route path='/'>
<Home
ingredients={ingredients}
total={() => calculate()}
ingredientAdd={(i) => ingredientAdd(i)}
ingredientRemove={(i) => ingredientRemove(i)}
selectedIngredients={ingredients}
isOrder={number}
/>
</Route>
</Switch>
</Router>
最佳答案
您正在尝试在 Router
之外使用 history
Hook 。将您的函数写入 Header
组件中。
在Header
组件中尝试这个
const history = useHistory();
const homeButtonOnClickHandler = () =>{
history.push('/');
}
关于javascript - useHistory - 无法读取未定义的属性 'push',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64701116/