[英]NextJs routing to the same page with different query
I have a web app, which has user profiles.我有一个 web 应用程序,其中包含用户配置文件。 Now a user has a profile, and the url would look something like:现在用户有一个配置文件,url 看起来像:
domain.com/profile?id=123 domain.com/profile?id=123
while self-profile looks like而自我介绍看起来像
domain.com/profile domain.com/profile
On mount (useEffect[]) I am fetching appropriate user data, eg.在安装 (useEffect[]) 时,我正在获取适当的用户数据,例如。 user data for query of user id 123 OR logged in user's profile, if both fail then reroute to main page.用于查询用户 id 123 或登录用户个人资料的用户数据,如果两者都失败,则重新路由到主页。
Now the problem comes when I want to go from现在问题来了,当我想从 go
domain.com/profile?id=123 domain.com/profile?id=123
to至
domain.com/profile domain.com/profile
Or any other profile with an ID.或任何其他具有 ID 的个人资料。
I can have solved the issue when a user is using browser's back (?id=123) and forward (?id=124) buttons to these routers:当用户使用浏览器的后退 (?id=123) 和前进 (?id=124) 按钮到这些路由器时,我可以解决这个问题:
router.beforePopState(({ url }) => {
const paramsString = _.replace(url, '/profile?', '');
const params = new URLSearchParams(paramsString);
const profileId = params.get('id');
const notEmptyProfileId = profileId && profileId !== '';
if (!notEmptyProfileId) userProfileFetchingFunction(profileId);
if (notEmptyProfileId) selfProfileFetchingFunction();
return true;
});
However, the issue.然而,问题。 Profile page has a NextJs's个人资料页面有一个 NextJs
<Link href="/profile" as="/profile"> My profile </Link>
And that is causing the problem.这就是问题的根源。 By pressing on it, it does NOT trigger rerender of the component, or get any props changed.通过按下它,它不会触发组件的重新渲染,也不会更改任何道具。
I can create something extra like我可以创造一些额外的东西
onClick=(() => Router.push('profile); reduxActionThatChangesStateOfCurrentProfileId();
but that's a bad fix.但这是一个糟糕的解决方案。 Any suggestions?有什么建议么?
Issue was with the wrapper I used for that page.问题在于我用于该页面的包装器。
What fixed it.什么修好了。
export default withRouter(
compose(
connect(
mapStateToProps,
mapDispatchToProps,
),
)(PageComponent));
What was before:以前是什么:
export default
compose(
connect(
mapStateToProps,
mapDispatchToProps,
),
)withRouter(PageComponent));
Redux was persisting the router... Not letting the props change, WOW. Redux 一直在坚持路由器......不让道具改变,哇。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.