简体   繁体   English

到达路由器导航更新 URL 但不是组件

[英]Reach router navigate updates URL but not component

I'm trying to get Reach Router to navigate programmatically from one of my components.我正在尝试让 Reach Router 从我的一个组件以编程方式导航。 The URL is updated as expected however the route is not rendered and if I look at the React developer tools I can see the original component is listed as being displayed. URL 已按预期更新,但未呈现路由,如果我查看 React 开发人员工具,我可以看到原始组件被列为正在显示。

If I refresh the page once at the new URL then it renders correctly.如果我在新 URL 处刷新页面一次,则它会正确呈现。

How can I get it to render the new route?我怎样才能让它渲染新路线?

A simplified example is shown below and I'm using @reach/router@1.2.1 (it may also be salient that I'm using Redux).下面显示了一个简化的示例,我使用的是@reach/router@1.2.1 (我使用 Redux 也可能很突出)。

import React from 'react';

import { navigate } from '@reach/router';

const ExampleComponent = props => {
  navigate('/a/different/url');

  return <div />;
};

export default ExampleComponent;

Could it be that you use @reach/router in combination with redux-first-history ?可能是您将@reach/routerredux-first-history结合使用吗? Because I had the same issue and could solve it with the following configuration of my historyContext:因为我遇到了同样的问题并且可以通过我的 historyContext 的以下配置来解决它:

import { globalHistory } from "@reach/router";
// other imports

const historyContext = createReduxHistoryContext({
   // your options...
   reachGlobalHistory: globalHistory // <-- this option is the important one that fixed my issue
}

More on this in the README of redux-first-historyredux-first-historyREADME 中有更多关于这个的信息

The same issue happens to me when I'm just starting to play around with Reach Router.当我刚开始使用 Reach Router 时,同样的问题发生在我身上。 Luckily, found the solution not long after.幸运的是,不久后找到了解决方案。

Inside Reach Router documentation for navigate , it is stated that:导航的Reach Router 文档中,指出:

Navigate returns a promise so you can await it. Navigate 返回一个 promise,因此您可以等待它。 It resolves after React is completely finished rendering the next screen, even with React Suspense.它在 React 完全完成下一个屏幕的渲染后解决,即使使用 React Suspense。

Hence, use await navigate() work it for me.因此,使用await navigate()为我工作。

import React, {useEffect} from 'react';
import {useStoreState} from "easy-peasy";
import {useNavigate} from "@reach/router";

export default function Home() {
    const {isAuthenticated} = useStoreState(state => state.auth)
    const navigate = useNavigate()

    useEffect(()=> {
        async function navigateToLogin() {
            await navigate('login')
        }
        if (!isAuthenticated) {
            navigateToLogin()
        }
    },[navigate,isAuthenticated])

    return <div>Home page</div>
}

I was running into the same issue with a <NotFound defualt /> route component.我遇到了与<NotFound defualt />路由组件相同的问题。

This would change the URL, but React itself didn't change:这会改变 URL,但 React 本身并没有改变:

import React from "react";
import { RouteComponentProps, navigate } from "@reach/router";

interface INotFoundProps extends RouteComponentProps {}

export const NotFound: React.FC<INotFoundProps> = props => {
  // For that it's worth, neither of these worked 
  // as I would have expected
  if (props.navigate !== undefined) {
    props.navigate("/");
  }
  // ...or...
  navigate("/", { replace: true });

  return null;
};

This changes the URL and renders the new route as I would expect:这会更改 URL 并按照我的预期呈现新路由:

...
export const NotFound: React.FC<INotFoundProps> = props => {
  React.useEffect(() => {
    navigate("/", { replace: true });
  }, []);

  return null;
};

Try and use gatsby navigate.尝试使用 gatsby 导航。 It uses reach-router.它使用到达路由器。 It solved my problem它解决了我的问题

import { navigate } from 'gatsby'从“盖茨比”导入{导航}

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

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