简体   繁体   English

是否可以通过React Router将Link中的props从组件传递到Link的组件?

[英]Is it possible to pass props from Link by React Router from a Component to the component by Link?

This is the code I want to do from the parent Component Home: 这是我想从父组件主目录执行的代码:

class Home extends Component {
render() {
    const { environment } = this.props; // <-- this is the props I want to pass
    <Link environment to="/register"> // <-- this is what I want to do
     <RaisedButton label="Register" style={{ margin: 12 }} />
    </Link>
}

And this is my React Router configuration code: 这是我的React Router配置代码:

ReactDOM.render(
   <BrowserRouter>
      <div>
      <Route exact path='/(index.html)?' component={Home}/>
      <Route  path='/register' component={Registration}/>
      </div>
    </BrowserRouter>,
  mountNode
);

There's a prop from the parent component Home that I want to pass on Registration component, How do I do that? 父组件Home中有一个我要传递给Registration组件的道具,该怎么办?

You can pass a state in the Link to object like so: 您可以在Link to object中传递状态,如下所示:

 <Link to={{
  pathname: '/register',
  state: { linkState: this.props.myProp }
}}/>

Then in the Registration component you can access this state like so: 然后,在注册组件中,您可以像下面这样访问此状态:

this.props.location.state.linkState

For more info see: https://reacttraining.com/react-router/web/api/Link 有关更多信息,请参见: https : //reacttraining.com/react-router/web/api/Link

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

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