首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用hookrouter制作自定义链接组件?

如何使用hookrouter制作自定义链接组件?
EN

Stack Overflow用户
提问于 2020-03-08 07:05:41
回答 1查看 271关注 0票数 1

我正在尝试创建一个自定义路由器组件,以便我的tr链接到通过该行的数据对象传递的另一个页面。当我点击这个链接时,我的not found页面就会出现,即使我使用'/‘这样的简单路径。我在为调试而制作的简化版本中也遇到了这个问题。下面是我的代码:

代码语言:javascript
复制
import {setLinkProps} from 'hookrouter'

 const MyLinkButton = (props) => {
        console.log(props)
        return <button {...setLinkProps(props)} className="myButton">Click me now</button>
    }

<MyLinkButton href="/" datathing='lol'></MyLinkButton>

(我确信这是我的一个错误,但是我还没有找到使用这个库的自定义路由器组件的任何代码示例进行比较,我也不能通过费力地阅读源代码来理解这个问题。)

EN

回答 1

Stack Overflow用户

发布于 2020-03-08 07:42:18

你能确认你是不是作为道具传进来的?根据代码,它看起来像是使用href键的期望和对象。您可以在注释中给出的示例中看到这一点。它们显式地具有{'href': route}。在您的示例中,您看起来像是在手动设置按钮本身中的href元素。当您单击该按钮时,您的控制台是否返回任何错误?

源代码中的注释注意到它

代码语言:javascript
复制
 * Accepts HTML `a`-tag properties, requiring `href` and optionally
 * `onClick`, which are appropriately wrapped to allow other
 * frameworks to be used for creating `hookrouter` navigatable links.
 *
 * If `onClick` is supplied, then the navigation will happen before
 * the supplied `onClick` action!
...
 * <MyFrameworkLink what="ever" {...useLink({ href: '/' })}>
 *   Link text
 * </MyFrameworkLink>
...
 * Accepts standard HTML `a`-tag properties. `href` and, optionally,
 * `onClick` are used to create links that work with `hookrouter`.
 * <A href="/" target="_blank">Home</A>

您的代码看起来几乎完全符合预期,所以我假设它是道具本身,而没有看到代码的其余部分。我还会删除控制台日志和返回关键字。

您可以找到custom link component here in the documentation的示例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60583023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档