reactjs - 如何使用 getProps 和 setState 更改 Gatsby Link 文本
问题描述
我的 Gatsby 网站的 header.js 包含一个带有徽标和链接的简单导航。如果“关于”页面处于活动状态:
- 链接文本应从“关于”更改为“关闭”
- 链接 URL 应从“/about”更改为“/”
请参阅下面的 header.js 代码:
import React, { useState } from "react"
import { Link } from "gatsby"
export default function Header() {
const [aboutLink, setAboutLink] = useState({
title: "About",
url: "/about",
})
const closeLink = {
title: "Close",
url: "/",
}
const isActive = ({ isCurrent }) => {
return isCurrent && setAboutLink(closeLink)
}
return (
<div id="header">
<Link to="/">My Website</Link>
<Link getProps={isActive} to={aboutLink.url}>{aboutLink.title}</Link>
</div>
)
}
我试图用它来实现这一点,setState
它似乎工作。但不正确,因为我也在控制台中收到此错误:
Warning: Cannot update a component (`Header`) while rendering a different component (`Context.Consumer`). To locate the bad setState() call inside `Context.Consumer`, follow the stack trace as described in https://github.com/facebook/react/issues/18178#issuecomment-595846312
in Location (created by Context.Consumer)
in Link (created by GatsbyLink)
in GatsbyLink (created by Context.Consumer)
in Location (created by GatsbyLinkLocationWrapper)
in GatsbyLinkLocationWrapper (created by ForwardRef)
in ForwardRef (at header.js:22)
in div (at header.js:20)
in Header (at layout.js:8)
in div (at layout.js:7)
in Layout (at about.js:21)
in About (created by HotExportedAbout)
in AppContainer (created by HotExportedAbout)
in HotExportedAbout (created by PageRenderer)
in PageRenderer (at query-result-store.js:90)
in PageQueryStore (at root.js:58)
in RouteHandler (at root.js:80)
in div (created by FocusHandlerImpl)
in FocusHandlerImpl (created by Context.Consumer)
in FocusHandler (created by RouterImpl)
in RouterImpl (created by Context.Consumer)
in Location (created by Context.Consumer)
in Router (at root.js:75)
in ScrollHandler (at root.js:71)
in RouteUpdates (at root.js:70)
in EnsureResources (at root.js:68)
in LocationHandler (at root.js:126)
in LocationProvider (created by Context.Consumer)
in Location (at root.js:125)
in Root (at root.js:134)
in StaticQueryStore (at root.js:150)
in ConditionalFastRefreshOverlay (at root.js:149)
in _default (at app.js:165)
我想不通。如果我useEffect
在此示例中使用https://flaviocopes.com/react-update-while-rendering-different-component/我会收到此错误'isActive' is not defined
:
useEffect(() => {
const isActive = ({ isCurrent }) => {
return isCurrent && setAboutLink(closeLink)
}
})
解决方案
您需要更改isActive
属性,isCurrent
或者使用isPartiallyCurrent
,正如您从文档中看到的那样:
Gatsby 的
<Link>
组件带有一个getProps
prop,它可以用于高级样式。它向您传递具有以下属性的对象:
isCurrent
— 如果 the与组件的 to proplocation.pathname
完全相同,则为 true<Link>
isPartiallyCurrent
—true
如果location.pathname
以<Link>
组件的 to 属性开头href
— to 的值prop
location
— 页面的location
对象你可以阅读更多关于它
@reach/router
的文档。
相关话题:
推荐阅读
- python - 它可以更干净或/和更少符合相同的功能吗?
- python - 如何加快 Python 中的嵌套 for?
- google-apps-script - 可安装在编辑触发器上,该触发器在一个单元格上触发,从另一个单元格获取数据并将数据写入第三个单元格
- javascript - 如何仅用 ES6 替换 Lodash 的 _.countBy?
- javascript - 仅提取数组中的多个对象
- scala - 在 Spark Scala 中获取具有最大日期的单行
- ios - 如何从 iOS Web 应用程序与蓝牙设备配对?
- mongodb - brew 服务启动 mongodb-community
- javascript - 什么会使我的节点应用停止在 Heroku 上运行?
- spring-data-jpa - 如何使用 Spring Data Rest 和 queryDSL 在连接表中查询多个值