简体   繁体   English

我如何 go 到盖茨比的上一页(history.goBack)

[英]How do I go to previous page in gatsby (history.goBack)

I am working on gatsby.我正在研究盖茨比。 I need to go back to privious page/link as I used to do with reactjs.我需要 go 回到原来的页面/链接,就像我以前用 reactjs 做的那样。

<a onClick={() => this.props.history.goBack}>
  <button type="button" className="close_tab">
    <img src={Close} alt="" />
  </button>
</a>

How can I do this using gatsby?我如何使用盖茨比做到这一点?

Use navigate(-1) :使用navigate(-1)

import React from "react";
import { navigate } from "gatsby";

export default function GoBack() {
  return (
    <Button onClick={() => navigate(-1)}>
      Go Back
    </Button>
  );
}

Edit : Since reach-router@1.3.0 , you can now simply call navigate(-1) to go back.编辑:由于reach-router@1.3.0 ,您现在可以简单地调用navigate(-1)返回。 Manually update reach-router in your Gatsby project if it's not yet updated.如果尚未更新,请手动更新 Gatsby 项目中的reach-router。 Thanks @nathan in the comment for the tip.感谢@nathan 在评论中的提示。


Edit : Ah alright, I've just realized this.props.history.goBack is a react-router thing.编辑:啊好吧,我刚刚意识到this.props.history.goBack是一个react-router东西。 Gatsby doesn't use react-router , but reach-router under the hood and it doesn't have the history props or the goBack method. Gatsby 不使用react-router ,而是在引擎盖下使用reach-router并且它没有history道具或goBack方法。 There's a issue requesting to add this , but wasn't implemented.有一个问题要求添加 this ,但没有实现。 You'd have to use browser's own history object as I suggested below.正如我在下面建议的那样,您必须使用浏览器自己的历史记录对象。

import React from 'react'

const BackButton = React.forwardRef(
  ({ children, ...props }, ref) => {
    const onClick = e => {
      e.preventDefault()
      history.back()
    }
    return (
      <a {...props} ref={ref} href="#" onClick={onClick}>
        {children}
      </a>
    )
  }
)

BackButton.displayName = 'BackButton'
export { BackButton }

Is this.props.history the browser's history? this.props.history是浏览器的历史记录吗? If so, you can do this.props.history.go(-1) to go back to the previous page.如果是这样,您可以执行this.props.history.go(-1)返回上一页。

As always with Gatsby, watch out when you use methods from browser, since they don't exist during html generation:与 Gatsby 一样,当您使用浏览器中的方法时要小心,因为它们在 html 生成期间不存在:

export default () => (
  <button onClick={() => {
    typeof history !== 'undefined' && history.go(-1)
  }}>back</button>
)

The gatsby navigate function is type as NavigateFn. gatsby 导航功能的类型为 NavigateFn。

Which you can find declare as:您可以找到声明为:

export interface NavigateFn {
    (to: string, options?: NavigateOptions<{}>): Promise<void>;
    (to: number): Promise<void>;
}

So, as you can see, you either can pass the route you want to redirect to, or an specific number.因此,如您所见,您可以传递要重定向到的路由或特定号码。

Try with navigate(-1)尝试使用navigate(-1)

对于 Gatsby 中的函数组件:

   <a onClick={() => window.history.back()}>Go back</a>

This should work这应该工作

import { navigate } from "@gatsbyjs/reach-router";

 <button onClick={() => navigate(-1)}>Back to previous page</button>

It goes to the previous page它转到上一页

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

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