[英]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...在这一点上,这里唯一的限制是设计......
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;
}
}
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;
}
}
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.