简体   繁体   English

在这种情况下如何使用“history.push”? 反应路由器dom V5

[英]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 外部的代码中使用。

    1. Create and export a custom history object to match the router being used.创建并导出自定义历史对象以匹配正在使用的路由器。

       import { createBrowserHistory } from 'history'; const history = createBroswerHistory(); export default history;
    2. 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> ); }
    3. 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.

相关问题 react-router-dom v5 - 链接有效,但 Redirect 和 history.push() 无效 - react-router-dom v5 - Link works, but Redirect and history.push() do not 如何在 React-router-dom 版本 6.0.0 中使用 history.push() - how to use history.push() in React-router-dom version 6.0.0 history.push 到 /login(注销时)在 react-router-dom 中不起作用 - history.push to /login (on logout) is not working in react-router-dom 可以覆盖 React-Router-Dom 的 History.push 方法 - Is possible to override History.push method of React-Router-Dom 未捕获的类型错误:history.push 不是 onClick 的 function (Home.js:22:1) 我正在使用 react-router-dom v6 - Uncaught TypeError: history.push is not a function at onClick (Home.js:22:1) i am using react-router-dom v6 history.push 使用 react-router-dom - history.push using react-router-dom react-router-dom-链接和history.push之间的区别? - react-router-dom - Difference between link and history.push? history.push(path)在react-router-dom v4中不起作用,在重定向到404时使用redux - history.push(path) is not working in react-router-dom v4, with redux while redirecting to 404 反应路由器 dom v5 history.push 不呈现新页面 - React router dom v5 history.push does not render new page 如何在react-router v5中使用history.push传递对象? - How to pass object with history.push in react-router v5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM