[英]How Can I Use "history.push" In This Situation? react-router-dom V5
I have a snippet of code that somewhat looks like this: https://codesandbox.io/s/youthful-field-4jwmxc?file=/src/SomeFile.js我有一段看起来像这样的代码: https ://codesandbox.io/s/youthful-field-4jwmxc?file=/src/SomeFile.js
// SomeFile.js
import { useHistory } from "react-router-dom";
/*
Cannot put it here either
const history = useHistory();
*/
export const SomeFile = () => {
console.log("something?");
handlers.SWITCH_PAGE();
};
const handlers = {
SWITCH_PAGE: () => {
try {
console.log("triggered");
/*
const history = useHistory();
Cannot put it here
*/
history.push({
pathname: "/someLocation"
});
} catch (e) {
console.error(e);
}
}
};
export default SomeFile;
This is just how the file was designed.这就是文件的设计方式。
I am attempting to make the history.push go to another page but due to the rules of React Hook, it will not let me.我试图让 history.push 转到另一个页面,但由于 React Hook 的规则,它不会让我这样做。
I tried putting the redirect in its own file but that also did not work: https://codesandbox.io/s/xenodochial-hypatia-c1fli1?file=/src/SomeFile.js我尝试将重定向放在自己的文件中,但这也不起作用: https ://codesandbox.io/s/xenodochial-hypatia-c1fli1?file=/src/SomeFile.js
// Redirect.js
import { useHistory } from "react-router-dom";
export const Redirect = (redirect) => {
function Redir(redirect) {
try {
const history = useHistory();
history.push(redirect);
} catch (e) {
console.error(e);
}
}
Redir(redirect);
return <></>;
};
export default Redirect;
// SomeFile.js
import Redirect from "./Redirect";
export const SomeFile = () => {
console.log("something?");
handlers.SWITCH_PAGE();
};
const handlers = {
SWITCH_PAGE: () => {
try {
console.log("triggered");
/*
history.push({
pathname: "/someLocation"
});
*/
Redirect("/someLocation");
} catch (e) {
console.error(e);
}
}
};
export default SomeFile;
What can I do to get around this?我能做些什么来解决这个问题?
If you need to access the history
object outside a React component you've basically a couple options.如果你需要访问 React 组件之外的
history
对象,你基本上有几个选择。
Create a global custom history object that your router uses and that can be imported for use in code external to React.创建你的路由器使用的全局自定义历史对象,并且可以导入以在 React 外部的代码中使用。
Create and export a custom history object to match the router being used.创建并导出自定义历史对象以匹配正在使用的路由器。
import { createBrowserHistory } from 'history'; const history = createBroswerHistory(); export default history;
Import the history
object and pass it to your app's router.导入
history
对象并将其传递给您的应用程序的路由器。
import { Router, // <-- low-level router Switch, Route } from "react-router-dom"; import history from "../path/to/history"; export default function App() { return ( <Router history={history}> // <-- pass history as prop <Switch> <Route path="/SomeLocation"> <SomeLocation /> </Route> <Route path="/"> <Home /> </Route> </Switch> </Router> ); }
Import the history
object to your utility code.将
history
对象导入您的实用程序代码。
import history from "../path/to/history"; export const someFile = () => { console.log("something?"); handlers.SWITCH_PAGE(); }; const handlers = { SWITCH_PAGE: () => { try { console.log("triggered"); history.push("/someLocation"); } catch (e) { console.error(e); } } }; export default someFile;
Pass history
in to the utility function for it to use.将
history
传递给实用程序函数以供其使用。
export const someFile = ({ history }) => { console.log("something?"); handlers.SWITCH_PAGE({ history }); }; const handlers = { SWITCH_PAGE: ({ history }) => { try { console.log("triggered"); history.push("/someLocation"); } catch (e) { console.error(e); } } }; export default someFile;
In component using the someFile
utility function, use the useHistory
hook to access the history
object and pass to the utility.在使用
someFile
实用程序函数的组件中,使用useHistory
挂钩访问history
对象并传递给实用程序。
import { useHistory } from 'react-router-dom'; import someFile from '../path/to/someFile'; const Component = () => { const history = useHistory(); ... const callback = () => { ... someFile(history); ... }; ... };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.