简体   繁体   English

将道具传递给React Router 4中的组件

[英]Passing props to component in React Router 4

I am new to react-router and I just started writing an app using react-router V4. 我是react-router的新手,我刚开始使用react-router V4编写应用程序。 I would like to to pass props to components rendered by <Match /> and I am wondering about what's the 'best' or 'proper' way to do so. 我想将道具传递给<Match />呈现的组件,我想知道什么是'最佳'或'正确'的方式。

Is it by doing something like this? 这是做这样的事吗?

<Match pattern="/" render={
    (defaultProps) => <MyComponent myProp = {myProp} {...defaultProps} />
}/>

Is this (passing props to components to be rendered by <Match /> ) even a good practice to do so with react-router or is it an antipattern or something; 这是(通过<Match />传递道具到组件)甚至是使用react-router这样做的好习惯,还是反模式或其他东西; and if so, why? 如果是这样,为什么?

You must use the render prop instead of component to pass on custom props, otherwise only default Route props are passed ( {match, location, history} ). 您必须使用render道具而不是component来传递自定义道具,否则只传递默认路径道具{match, location, history} )。

I pass my props to the Router and child components like so. 我将我的道具传递给路由器和子组件,如此。

class App extends Component {

  render() {
    const {another} = this.props
    return <Routes myVariable={2} myBool another={another}/>
  }
}

const Routes = (props) =>
  <Switch>
    <Route path="/public" render={ (routeProps) => 
      <Public routeProps={routeProps} {...props}/>
    }/>
    <Route path="/login" component={Login}/>
    <PrivateRoute path="/" render={ (routeProps) =>
       ...
    }/>
  </Switch>
render() {
  return (
    <Router history={browserHistory}>
      <Switch>
        <Route path="/" 
           render={ ()  => <Header 
             title={"I am Title"} 
             status={"Here is my status"}
           /> }
        />
        <Route path="/audience" component={Audience}/>
        <Route path="/speaker" component={Speaker}/>
      </Switch>
    </Router>
  )
}

I'm fairly new to react-router and came across a similar issue. 我对react-router很新,遇到了类似的问题。 I've created a wrapper based on the Documentation and that seems to work. 我已经基于文档创建了一个包装器,它似乎有效。

// Wrap Component Routes
function RouteWrapper(props) {
  const {component: Component, ...opts } = props

  return (
   <Route {...opts} render={props => (
     <Component {...props} {...opts}/>
   )}/>
 )
}

 <RouteWrapper path='/' exact loggedIn anotherValue='blah' component={MyComponent} />

So far so good 到现在为止还挺好

I use render in combination with a defined method like so: 我将render与定义的方法结合使用,如下所示:

class App extends React.Component {
  childRoute (ChildComponent, match) {
    return <ChildComponent {...this.props} {...match} />
  }

  render () {
    <Match pattern='/' render={this.childRoute.bind(this, MyComponent)} />
  }
}

render道具用于编写内联匹配,因此您的示例是传递额外道具的理想方式。

I'll do it like the following to improve clarity. 我将像下面这样做以提高清晰度。

const myComponent = <MyComponent myProp={myProp} {...defaultProps} />


<Match pattern="/" component={myComponent} />

By this your router code won't get messed up!. 这样你的router代码就不会搞砸了!

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

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