简体   繁体   English

NextJs中如何通过多层组件传递href值?

[英]How to pass the href value through multiple layers of components in NextJs?

So I am fairly new to NextJs and have been struggling to make this work over the past few days.所以我对 NextJs 还很陌生,并且在过去的几天里一直在努力完成这项工作。 Scrutinised the internet and haven't found someone with the same issue, so I am probably misunderstanding the workings of NextJs.仔细检查了互联网并没有发现有同样问题的人,所以我可能误解了 NextJs 的工作原理。

My file structure is as follows:我的文件结构如下:

//index.js

export default function Home() {

  return ( 
    <div className={classnames(indexStyles.wrapper, "gap")}>

        <div className={indexStyles.itemswrapper}>
          <Item image={banner1} text="Trusted by our clients" 
          button="View project" href="/project1" />

          <Item image={banner2} text="Effective design" 
          button="View project" href="/project 2" />
        </div>
  )
}

This is my index file (homepage) where I have multiple items which are components as I want to have different contents within them.这是我的索引文件(主页),其中我有多个作为组件的项目,因为我希望它们中有不同的内容。 This is what the item component looks like:这是 item 组件的样子:

// Item.js

const Item = ( props, { href } ) => {

    return (
      <div className={classnames(itemStyles.wrapper, "wrap", "center")}>
                
            <div className={itemStyles.thumbnail}>
                  <div className={itemStyles.image}>
                      <Image src={props.image} alt=""/>
                  </div>
            </div>

            <div className={itemStyles.cta_wrapper}>
                  <div className={itemStyles.title}>
                      <h2>{props.text}</h2>
                  </div>

                  <div className={itemStyles.knop}>
                      <Button href={{href}} ><a>{props.button}</a></Button>
                  </div>
            </div>

      </div>
    )
}

And this is what the Button component looks like:这就是 Button 组件的样子:

// Button.js

const Button = ({ children, href }) => {

    return (
        <div className={buttonStyles.button}>
            <Link href={{href}}><a>{children}</a></Link>
        </div>
    )
}

So my question is, how do I pass the href in the index.js page as a prop to the Item component so that the href in the Link in Button.js gets changed.所以我的问题是,如何将 index.js 页面中的 href 作为道具传递给 Item 组件,以便 Button.js 中的 Link 中的 href 得到更改。 As far as I found it has something to do with next router.据我发现它与下一个路由器有关。 I hope my question is clear, if not please do not refrain from asking further explanation.我希望我的问题很清楚,如果没有,请不要再询问进一步的解释。

PS. PS。 I left our all the imports at the top of each file in this question to make everything more clear, but everything has been imported correctly.我把所有的导入都留在了这个问题的每个文件的顶部,以使一切更清楚,但一切都已正确导入。

In Item.js href is just a regular prop, there's nothing special about it.Item.js中, href只是一个普通的道具,没有什么特别之处。

// Item.js // 项目.js

const Item = ( props = {} ) => {
    const {href} = props;
    return (
      <div className={classnames(itemStyles.wrapper, "wrap", "center")}>
                
            <div className={itemStyles.thumbnail}>
                  <div className={itemStyles.image}>
                      <Image src={props.image} alt=""/>
                  </div>
            </div>

            <div className={itemStyles.cta_wrapper}>
                  <div className={itemStyles.title}>
                      <h2>{props.text}</h2>
                  </div>

                  <div className={itemStyles.knop}>
                      <Button href={href}><a>{props.button}</a></Button>
                  </div>
            </div>

      </div>
    )
}

You would probably want to destructure the other props as well您可能还想解构其他道具

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM