简体   繁体   English

单击按钮时反应路由器获得正确的显示页面

[英]react router getting the right display page when click button

I have a question regrading to react.我有一个问题需要重新评分。 I have a IconButton (Material UI) I want to click this button and it will redirect to the specific help page.我有一个 IconButton (Material UI) 我想点击这个按钮,它会重定向到特定的帮助页面。

For example:例如:

  1. local:3000/education.本地:3000/教育。 When I clicked button it will redirect something like local:3000/education/support page.当我单击按钮时,它会重定向类似于 local:3000/education/support 页面的内容。
  2. local:3000/data.本地:3000/数据。 It redirect to something like data/support page.它重定向到数据/支持页面之类的东西。 ...etc more pages ...等更多页面

I am stuck, I am not sure how to implement the button onClick function to redirect to the right support page.我被卡住了,我不确定如何实现按钮 onClick function 以重定向到正确的支持页面。 I am thinking using react-router, I have did some research we can use 'params' and 'history' parameters from the props?我正在考虑使用 react-router,我做了一些研究,我们可以使用道具中的“参数”和“历史”参数? Thank you谢谢

Please give me some good advice and right direction thank you.请给我一些好的建议和正确的方向谢谢。

import { useLocation, useHistory } from "react-router-dom";

export default function App() {
  const history = useHistory();
  const { pathname } = useLocation();

  return (
    <div className="App">
      <button onClick={() => history.push(`${pathname}/support`)}>
        support page
      </button>
    </div>
  );
}

localhost:3000/education.本地主机:3000/教育。 If you use history.push("/support"), It redirects to localhost:3000/support.如果你使用 history.push("/support"),它会重定向到 localhost:3000/support。 But If you use history.push("support"), It redirects to localhost:3000/education/support但是如果你使用history.push("support"),它会重定向到localhost:3000/education/support

https://reactrouter.com/web/api/Hooks https://reactrouter.com/web/api/Hooks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM