【问题标题】:How to get previous url in react gatsby如何在反应盖茨比中获取以前的网址
【发布时间】:2019-07-23 08:00:48
【问题描述】:

我对@9​​87654322@ 非常熟悉,但对Gatsby 很陌生。

我想检测Gatsby中的上一页URL?

【问题讨论】:

  • 如果它使用浏览器的历史记录,prev url应该保存在document.referrer我认为。
  • 不,它是空白的。我没有得到任何东西。
  • @soroushchehresa 不,这不是重复的。请参考问题。我必须处理 gatsby 和我没有放 javascript 标签的事件。而且 gatsby build 也不会执行 window 变量。所以请收回你的票数。

标签: javascript reactjs gatsby static-site


【解决方案1】:

我用下面的代码解决了我的问题。这是参考链接https://github.com/gatsbyjs/gatsby/issues/10410

// gatsby-browser.js
exports.onRouteUpdate = () => {
  window.locations = window.locations || [document.referrer]
  locations.push(window.location.href)
  window.previousPath = locations[locations.length - 2]
}

现在你可以得到previousPath可以从任何地方访问。

【讨论】:

    【解决方案2】:

    这些答案部分正确。如果您使用链接 api 设置状态,则该状态会保留在浏览器历史记录中。

    因此,如果您从Page1 转到Page2,则例如state.prevUrl 将正确设置为Page1

    但如果你从Page2 转到Page3,然后再浏览器,那么state.prevUrl 仍然是Page1,这是错误的。

    我发现处理此问题的最佳方法是在 gatsby-browser.js 中添加类似的内容

    export const onRouteUpdate = ({ location, prevLocation }) => {
      if (location && location.state)
        location.state.referrer = prevLocation ? prevLocation.pathname : null
    }
    

    这样,您将始终在位置上使用以前的网址。

    【讨论】:

      【解决方案3】:

      您可以使用Link 组件传递状态:

      import React from 'react';
      import { Link } from 'gatsby';
      
      const PrevPage = () => (
        <div>
          <Link
            to={`/nextpage`}
            state={{ prevPath: location.pathname }}
          >
            Next Page
          </Link>
        </div>
      )
      
      const NextPage = (props) => (
        <div>
          <p>previous path is: {props.location.state.prevPath}</p>
        </div>
      );
      

      然后您可以在下一页中从this.props.location.state 访问prevPath

      【讨论】:

      • 谢谢!!!盖茨比无法做到这一点,因此我会接受你的上述答案
      • @Profer 别提了 ;)
      • @Profer 为什么你说 Gatsby 没有办法做到这一点?这个答案向您展示了如何使用 Gatsby 的 Link 组件(它建立在 Reach Router Link 组件之上)。
      • @coreyward 因为上面的答案只是一个技巧。如果有办法,那么 Gatsby 本身会提供一些道具或常量。
      • @Profer 这有什么技巧? Gatsby 本身在Link 上提供了state 属性,并将传递给它的值提供给下一页的页面组件。 It's documented here..
      【解决方案4】:

      完全归功于@soroushchehresa's answer——这个答案只是建立在它之上的额外内容。

      Gatsby 将在生产构建期间抛出错误,因为 location 在服务器端渲染期间不可用。您可以通过首先检查 window 对象来绕过它:

      class Page extends React.Component {
        state = {
          currentUrl: '',
        }
      
        componentDidMount() {
          if (typeof window == 'undefined') return
          this.setState({ currentUrl: window.location.href })
        }
      
        render() {
          return (
            <Link to="..." state={{ prevUrl: this.state.currentUrl }}>
          )
        }
      }
      
      

      但这需要我们在每个页面上都实现这一点,这很乏味。 Gatsby 已经为服务器端渲染设置了@reach/router,所以我们可以挂钩它的location 属性。只有路由器组件才能获得该道具,但我们可以使用@reach/router's Location 组件将其传递给其他组件。

      这样,我们可以编写一个自定义的 Link 组件,该组件总是在其状态下传递先前的 url:

      // ./src/components/link-with-prev-url.js
      
      import React from 'react'
      import { Location } from '@reach/router'
      import { Link } from 'gatsby'
      
      const LinkWithPrevUrl = ({ children, state, ...rest }) => (
        <Location>
          {({ location }) => (
                            //make sure user's state is not overwritten
            <Link {...rest} state={{ prevUrl: location.href, ...state}}>
              { children }
            </Link>
          )}
        </Location>
      )
      
      export { LinkWithPrevUrl as Link }
      

      然后我们可以导入我们自定义的 Link 组件,而不是 Gatsby 的 Link:

      -  import { Link } from 'gatsby'
      +  import { Link } from './link-with-prev-url'
      

      现在每个 Gatsby 页面组件都会得到这个之前的 url 属性:

      const SomePage = ({ location }) => (
        <div>previous path is {location.state.prevUrl}</div>
      );
      

      您也可以考虑为客户端创建一个存储状态的容器,并在gatsby-ssr.jsgatsby-browser.js 中使用wrapRootElementwrapPageElement

      【讨论】:

      • 光滑。我喜欢它。
      • 实际上,我通过 Gatsby 文档找到了最初的答案,并希望避免乏味。感谢您对其进行阐述以创建一种简化的方法——好主意!我正在查看 gatsby-browser.js 中的 onRouteUpdate API,它公开了 location, prevLocation,但据我所知,它只使它们可用于我的 Gatsby/React 组件范围之外。
      猜你喜欢
      • 2017-02-22
      • 2022-01-23
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2020-03-16
      • 2021-01-14
      相关资源
      最近更新 更多