简体   繁体   English

组件不呈现使用时创建的最新数据<Link>

[英]Component not rendering latest data created when using <Link>

What is difference in React between writing url directly and using the below link. React 中直接写 url 和使用下面的链接有什么区别。

Here is some code:这是一些代码:

<Link onClick={() => history.push('/notification')}>My alerts</Link>

Here is the component that is redirected to when user click link my alerts.这是当用户单击链接我的警报时重定向到的组件。

const { data: employer, loading, error, refetch } = useQuery<Account>(AccountQuery)
  const [deleteSearchSubscription] = useMutation(DeleteSearchSubscriptionMutator, {
    onError: error => console.error(error),
    onCompleted: () => refetch(),
  })

UPDATE:更新:

<Router>
        <Header />
        <Page>
          <Switch>
            <Route path="/notification">
              <EmailNotifications />
            </Route>
            <Route path="/error-page">
              <PageNotFound />
            </Route>
            <Route path="/not-found">
              <ErrorPage />
            </Route>
            <Route path="/login">
              <Login />
            </Route>
          </Switch>
        </Page>
        <Footer />
      </Router>

any idea why when writing directly to url my component gets rendered with the latest data from backend while clicking on link it does not get the latest data from backend, why is this behavior?知道为什么当直接写入 url 时,我的组件在单击链接时使用后端的最新数据呈现,但它没有从后端获取最新数据,为什么会出现这种行为?

I think we need to see more of your code, but are you trying to refresh?我认为我们需要查看更多您的代码,但您是否正在尝试刷新? Because that link look good even I think this is better form因为那个链接看起来不错,即使我认为这是更好的形式

<Link to="/notification">My alerts</Link>

But if you are trying to reload and you are already on yoursite.com/notification , it will not work但是,如果您正在尝试重新加载并且您已经在yoursite.com/notification 上,它将无法正常工作

But I cannot tell from the code you've provided if this is the case但我无法从您提供的代码中判断是否是这种情况

If yes you need to redirect to other path and than back to /notification如果是,您需要重定向到其他路径而不是返回到 /notification

UPDATE:更新:

What Router do you use?你用什么路由器?

Basic Route component from React-router looks like this React-router 的基本路由组件如下所示

<Route path="/notification" component={EmailNotifications} /> 

With no children没有孩子

See: https://reacttraining.com/react-router/web/api/Route/component请参阅: https : //reacttraining.com/react-router/web/api/Route/component

Link has props to .链接有道具to . So you don't need onClick所以你不需要onClick

<Link to='/notification'>My alerts</Link>

adding:添加:

  useEffect(() => {
    refetch()
  })

fixed the issue!修复了问题!

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

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