按下主页按钮后,我要将其重定向到主页“ /”。但我收到此错误。我能做什么 ? :
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>
答案 0 :(得分:1)
您只需要将钩子和处理程序放入组件中即可,例如:link
答案 1 :(得分:1)
您正在尝试在history
之外使用Router
钩子。在Header
组件中编写函数。
在Header
组件内部尝试
const history = useHistory();
const homeButtonOnClickHandler = () =>{
history.push('/');
}