我看过很多关于这个问题的答案,但还是想不出
我正在使用
`" "react-router": "^5.2.1",
"react-router-dom": "^5.2.1",`
// app.js
import history from "./history";
import { Router } from 'react-router';
export const App = () => {
return (
<ReduxProvider store={store}>
<Router history={history}>
<div>
<Route path="/first/" component={MN} />
<Route path="/test/" component={Test} />
</div>
</Router>
</ReduxProvider>
);
};
// history.js
import { createBrowserHistory } from 'history';
export default createBrowserHistory();
//在组件中调用
import history from "./history";
....
history.push('mn/testing')
//mn.js
import history from "../history";
const Mn = () => {
return (
<div>
<Router history={history}>
<Route exact path="*/testing" component={DashBoard} />
/>
</Router>
</div>
);
编辑:我有嵌套的路由,这是一个错误吗?
发布于 2021-10-06 02:27:54
您应该尝试使用react-router提供的useHistory()
钩子
import { useHistory } from "react-router-dom";
function HomeButton() {
const history = useHistory();
function handleClick() {
history.push("/home");
}
return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}
https://stackoverflow.com/questions/69463946
复制相似问题