【问题标题】:Nested route with dinamically generated link with reach router in Gatsby具有动态生成链接的嵌套路由与 Gatsby 中的到达路由器
【发布时间】:2021-02-19 14:00:08
【问题描述】:

我有一个由 gatsby CreatePage 生成的页面,其中包含我使用reach/router 设置路由器的模板。

为了说明问题,下面有一个简化版的代码。 路由器有两个嵌套路由:

MyTemplate.js

 ...   
    <Router basepath={`/${slug}`}>
      <CategoryBar path= "/"> 
        <PostSelector path= "works" category= {category}>   
          <Post path= "test" category={category}></Post>
        </PostSelector>
      </CategoryBar>
    </Router>

&lt;PostSelector&gt; 中,组件链接是通过 .map 方法动态生成的。

问题: 看起来 {props.children} 没有通过 dowm;链接已生成并呈现,但单击它们不会呈现&lt;Post&gt; 组件。

这里是&lt;PostSelector&gt;的代码

PostSelector.js

...
const PostSelector =( {category}, props) => { 
        return(   
            category.edges.map((post, i) => (
                <div key={i}>
                    <ul>
                    <Link to="test">{post.node.title}</Link> 
                    </ul>
                {props.children}
                </div>
                
            ))
        )
}

如果我用硬编码链接替换动态生成的链接,它会按预期工作,将&lt;Post&gt; 渲染为&lt;PostSelector&gt; 的子级

任何帮助将不胜感激

【问题讨论】:

    标签: javascript reactjs hyperlink gatsby reach-router


    【解决方案1】:

    用途:

    const PostSelector =( {category, children}) => { 
            return(   
                category.edges.map((post, i) => (
                    <div key={i}>
                        <ul>
                        <Link to="test">{post.node.title}</Link> 
                        </ul>
                    {children}
                    </div>
                    
                ))
            )
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 1970-01-01
      • 2014-02-13
      • 2021-08-11
      • 2020-02-29
      • 1970-01-01
      • 2017-10-22
      • 2018-04-09
      • 2019-08-28
      相关资源
      最近更新 更多