简体   繁体   English

在 this.props.history.push 上刷新页面,使用相同的 url

[英]Refresh page on this.props.history.push with same url

I currently have a functionality that pushes users to different pages with history.push.我目前有一个功能,可以通过 history.push 将用户推送到不同的页面。 Is it possible if the user is in the current path and decides to click the same path to re-render the current page?如果用户在当前路径中并决定单击相同的路径以重新呈现当前页面,是否有可能?

Example, from the code below.例如,来自下面的代码。 If a user is in /home/reportanissue/createissue, if they decided to click the same url path, the expected behavior is to re-render the page.如果用户在 /home/reportanissue/createissue 中,如果他们决定单击相同的 url 路径,则预期的行为是重新呈现页面。

Thanks谢谢

handleMenuItemClick(event) {
    switch (event.currentTarget.id) {
      case "mainPage":
        this.props.history.push("/home/reportanissue");
        break;
      case "myTasks":
        this.props.history.push("/home/reportanissue/managemytasks");
        break;
      case "myQueues":
        this.props.history.push("/home/reportanissue/myqueues");
        break;
      case "createNewIssue":
        this.props.history.push("/home/reportanissue/createissue");
        break;
      case "submitException":
        this.props.history.push("/home/reportanissue/submitnewexception");
        break;
      default:
        return;
    }
  }

This behavior is easy to create by using location.pathname to match the path and window.location.reload() to refresh the page.通过使用location.pathname匹配路径并使用window.location.reload()刷新页面,可以轻松创建此行为。

handleMenuItemClick(event) {
    switch (event.currentTarget.id) {
      case "mainPage": {
        if (this.props.location.pathname === "/home/reportanissue") {
          window.location.reload();
          break;
        }
    
        this.props.history.push("/home/reportanissue");
        break;
      ...
      default:
        return;
    }
}

At this point, the only limitations here are design...在这一点上,这里唯一的限制是设计......

  1. DRY up the code:干掉代码:
const PATH_LINKS = {
  mainPage: "/home/reportanissue",
  myTasks: "/home/reportanissue/managemytasks",
  ...
}

handleMenuItemClick(event) {
    const { history, location } = this.props;
    const key = event.currrent.target.id;
    
    switch (key) {
      case "mainPage": {
        if (location.pathname === PATH_LINKS[key]) {
          window.location.reload();
          break;
        }
    
        history.push(PATH_LINKS[key]);
        break;
      case "myTasks": {
        if (location.pathname === PATH_LINKS[key]) {
          window.location.reload();
          break;
        }
    
        history.push(PATH_LINKS[key]);
        break;
      default:
        return;
    }
}
  1. Simplify the pattern:简化模式:
const PATH_LINKS = {
  mainPage: "/home/reportanissue",
  myTasks: "/home/reportanissue/managemytasks",
  ...
}

handleMenuItemClick(event) {
    const { history, location } = this.props;
    const key = event.currrent.target.id;
    
    switch (key) {
      case "myPage":
      case "myTasks": {
        if (location.pathname === PATH_LINKS[key]) {
          window.location.reload();
          break;
        }
    
        history.push(PATH_LINKS[key]);
        break;
      default:
        return;
    }
}
  1. Avoid the switch/case all together:一起避免switch/case
const PATH_LINKS = {
  mainPage: "/home/reportanissue",
  myTasks: "/home/reportanissue/managemytasks",
  ...
}

handleMenuItemClick(event) {
    const { history, location } = this.props;
    const key = event.currrent.target.id;

    if (location.pathname === PATH_LINKS[key]) {
      return window.location.reload();
    } else if (PATH_LINKS[key]) {
      return history.push(PATH_LINKS[key]);
    }

    console.error("The following path does not exist :", key);

}

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

相关问题 this.props.history.push() 正在更改 URL 但不执行下一页的操作 - this.props.history.push() is changing the URL but not doing the action to the next page this.props.history.push刚刚刷新后正在运行 - this.props.history.push is working just after refresh ReactJs this.props.history.push()不起作用 - ReactJs this.props.history.push() is not working 当我在同一路由中传递回调函数时 this.props.history.push() 不起作用 - this.props.history.push() is not working when i pass the callback function in same route 成功登录后, this.props.history.push 不会进入新页面,因为 ProtectedRoute 不成功 - On successful login, this.props.history.push does not take to new page as ProtectedRoute is not successful 类型错误:无法使用 this.props.history.push 读取未定义的属性“推送” - TypeError: Cannot read property 'push' of undefined with this.props.history.push this.props.history.push 适用于某些组件而不适用于其他组件 - this.props.history.push works in some components and not others React-router v4 this.props.history.push(...) 不工作 - React-router v4 this.props.history.push(...) not working TypeError: undefined is not an object (评估 'this.props.history.push') - TypeError: undefined is not an object (evaluating 'this.props.history.push') this.props.history.push("/") 没有将我重定向到主页 - this.props.history.push("/") isn't redirecting me to homepage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM