简体   繁体   English

链接到相同的React组件时传递道具

[英]Passing props when linking to same react component

I want to link to the same component by clicking on different links but I want to return different things depending on the link I click on. 我想通过单击不同的链接来链接到相同的组件,但是我想根据单击的链接返回不同的内容。

so I have this: 所以我有这个:

{this.props.children}
<Link to="channel"> General </Link>
<br />
<Link to="channel"> Random </Link>

Then in my render I have this 然后在我的渲染中我有这个

<Route path="channel" foo="General" component={Channel}></Route>
<Route path="channel" foo="Random" component={Channel}></Route>

That calls the Channel component: 调用Channel组件:

export default class Channel extends React.Component{

  render(){
    return (
      <h1> {this.props.route.foo} </h1>

    )
  }
}

But I want it to return the value of the prop foo but every time it returns "General". 但是我希望它返回prop foo的值,但是每次它返回“ General”时。 How can I link the routing to the <Link to part? 如何将布线<Link to零件?

Your paths should be different. 您的路径应该不同。 Both routes have path="channel" and only the first route is registered by react-router. 这两个路由都具有path="channel" ,只有第一个路由由react-router注册。 Do something like: 做类似的事情:

<Route path="channel" foo="General" component={Channel}></Route>
<Route path="channel2" foo="Random" component={Channel}></Route>

And your links should be: 您的链接应该是:

{this.props.children}
<Link to="channel"> General </Link>
<br />
<Link to="channel2"> Random </Link>

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

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