简体   繁体   English

在页面加载时选择了 NavLink activeClassName

[英]NavLink activeClassName selected on page load

Is it possible to push with history.push the url of the route on useEffect to maintain selected a Navlink on page load?是否可以使用 history.push 在 useEffect 上推送路由的 url 以在页面加载时保持选定的 Navlink?

Navigation.tsx导航.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!谢谢!

Issuing the navigation action from the useEffect hook is completely valid and allowed.useEffect挂钩发出导航操作是完全有效且允许的。 Though, since it seems this Navigation 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.虽然,由于看起来这个Navigation组件实际上只是调用一些 API 来填充一些本地状态并无条件导航到其中一个子路由,我可能会建议使用重定向(REPLACE)而不是导航(PUSH)动作来帮助减少历史堆栈上的条目。

const { replace } = useHistory();

useEffect(() => {
  getCreatedEventsByUserId(userId).then(setOwnEvents);
  getAssistedEventsByUserId(userId).then(setAttendedEvents);
  replace(`${url}/created-events`);
}, [userId, history, url]);

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 the path to "{path}/created-events" .您可以改为渲染Redirect ,以从path重定向到"{path}/created-events"

useEffect(() => {
  getCreatedEventsByUserId(userId).then(setOwnEvents);
  getAssistedEventsByUserId(userId).then(setAttendedEvents);
}, [userId, history, url]);

...

<Switch>
  <ProtectedRoute exact path={`${path}/created-events`}>
    {attendedEvents && <EventsList events={attendedEvents} />}
  </ProtectedRoute>
  <ProtectedRoute exact path={`${path}/assisted-events`}>
    {ownEvents && <EventsList events={ownEvents} />}
  </ProtectedRoute>
  <Redirect to={`${path}/created-events`} />
</Switch>

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

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