在页面加载上选择的NavLink ActiveClassName
是否可以推动历史记录。用使用效率路由的URL在页面加载上维护选定的navLink?
navigation.tsx
export const Navigation = ({ userId }: { userId: Id }) => {
const [ownEvents, setOwnEvents] = useState<EventsPerMonth[]>();
const [attendedEvents, setAttendedEvents] = useState<EventsPerMonth[]>();
const { url, path } = useRouteMatch();
const { push } = useHistory()
useEffect(() => {
getCreatedEventsByUserId(userId).then(setOwnEvents);
getAssistedEventsByUserId(userId).then(setAttendedEvents);
push(`${url}/created-events`);
}, [userId, push, url]);
return (
<>
<StyledNavigation>
<ul>
<li>
<StyledNavLink to={`${url}/created-events`} activeClassName="any">
Eventos creados
</StyledNavLink>
</li>
<li>
<StyledNavLink to={`${url}/assisted-events`} activeClassName="any">
Eventos asistidos
</StyledNavLink>
</li>
</ul>
</StyledNavigation>
<Switch>
<ProtectedRoute exact path={`${path}/created-events`}>
{attendedEvents && <EventsList events={attendedEvents} />}
</ProtectedRoute>
<ProtectedRoute exact path={`${path}/assisted-events`}>
{ownEvents && <EventsList events={ownEvents} />}
</ProtectedRoute>
</Switch>
</>
);
};
允许?
useEffect(() => {
getCreatedEventsByUserId(userId).then(setOwnEvents);
getAssistedEventsByUserId(userId).then(setAttendedEvents);
push(`${url}/created-events`);
}, [userId, push, url]);
还有其他方法吗?
谢谢!
Is it possible to push with history.push the url of the route on useEffect to maintain selected a Navlink on page load?
Navigation.tsx
export const Navigation = ({ userId }: { userId: Id }) => {
const [ownEvents, setOwnEvents] = useState<EventsPerMonth[]>();
const [attendedEvents, setAttendedEvents] = useState<EventsPerMonth[]>();
const { url, path } = useRouteMatch();
const { push } = useHistory()
useEffect(() => {
getCreatedEventsByUserId(userId).then(setOwnEvents);
getAssistedEventsByUserId(userId).then(setAttendedEvents);
push(`${url}/created-events`);
}, [userId, push, url]);
return (
<>
<StyledNavigation>
<ul>
<li>
<StyledNavLink to={`${url}/created-events`} activeClassName="any">
Eventos creados
</StyledNavLink>
</li>
<li>
<StyledNavLink to={`${url}/assisted-events`} activeClassName="any">
Eventos asistidos
</StyledNavLink>
</li>
</ul>
</StyledNavigation>
<Switch>
<ProtectedRoute exact path={`${path}/created-events`}>
{attendedEvents && <EventsList events={attendedEvents} />}
</ProtectedRoute>
<ProtectedRoute exact path={`${path}/assisted-events`}>
{ownEvents && <EventsList events={ownEvents} />}
</ProtectedRoute>
</Switch>
</>
);
};
Is allowed?
useEffect(() => {
getCreatedEventsByUserId(userId).then(setOwnEvents);
getAssistedEventsByUserId(userId).then(setAttendedEvents);
push(`${url}/created-events`);
}, [userId, push, url]);
Is there any other way to do that?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
使用
挂钩上发出导航操作是完全有效并允许的。但是,由于似乎此导航
组件实际上只是调用一些API来填充某些本地状态,并且无条件导航到其中一条路线,所以我可能建议使用重定向(替换)而不是导航(推动)操作,以帮助减少历史记录堆栈的条目。实际上,此重定向似乎只能使用户进入子路由。您可以将
redirect
渲染,以从路径
重定向到“ {path}/create-events”
。Issuing the navigation action from the
useEffect
hook is completely valid and allowed. Though, since it seems thisNavigation
component is really just calling some APIs to populate some local state and unconditionally navigating to one of the children routes, I'd probably suggest using a redirect (REPLACE) instead of a navigation (PUSH) action to help cut down on entries on the history stack.In fact, this redirect seems to only serve to get the user to the sub-route. You could render a
Redirect
instead, to redirect from thepath
to"{path}/created-events"
.