gatsby - Gatsby 中的到达路由器链接闪烁页面然后消失
问题描述
我在 Gatsby 中使用 Reach Router 来设置客户端路由,如下所示,但是当我单击“关于”页面的链接时,它会从“关于”组件中闪烁内容然后消失。
import React from "react"
import { Router, Link } from "@reach/router"
import About from "../components/about.js"
import Layout from "../components/layout"
const IndexPage = () => (
<Layout>
<Link to="about">About</Link>
<Router>
<MapContainer path="/" />
<About path="/about" />
</Router>
</Layout>
)
export default IndexPage
我还发现,当我import { Link } from 'gatsby'
而不是 时import { Link } from '@reach/router'
,它会将我直接带到 Gatsby 的 404 页面。
在这两种情况下,浏览器上的后退按钮都会让我回到主页(显示 MapContainer),我认为这意味着该链接正在尝试查找不存在的静态页面。
解决方案
Gatsby 的路由扩展自 React 路由,因此您无需从 React 导入它。Gatsby's 具有相同的行为以及其他一些功能。
正如您所指出的,您应该使用<Link>
from Gatsby 而不是@reach/router
. 您的问题来自链接的路径,因为您以绝对方式发布它会导致404
页面。它应该看起来像:
import React from "react"
import {Link} from 'gatsby'
import About from "../components/about.js"
import Layout from "../components/layout"
const IndexPage = () => (
<Layout>
<Link to="/about">About</Link>
<Link to="/">Home</Link>
</Layout>
)
export default IndexPage
请注意相对路径(以斜杠开头/
)和路由器删除,因为它是多余的。
推荐阅读
- opencv - 从 64 位切换到 32 位 openCV 时出现链接错误
- gitlab - 使用本地 Jekyll 安装测试 Gitlab 页面时集成静态内容
- assembly - 搜索字符串中出现的字符数
- c# - C# WinForms 中 Windows Media Player 元素上的透明按钮
- python - 在 django 中覆盖 UserChangeForm
- c - C scanf第二次跳过
- c++ - cygwin_exception::open_stackdumpfile:将堆栈跟踪转储到 class4.exe.stackdump
- php - 使用 unix 选择上个月的行
- ios - Swift 4.2 类型“NSAttributedString.Key”没有成员“accessibilitySpeechPitch”
- vb.net - 如何在 Visual Basic 中跨类共享变量?