简体   繁体   English

React Router-将道具传递给子组件

[英]React router - pass props on routes to child components

Can someone please tell me the best way to do this? 有人可以告诉我最好的方法吗? I would like to pass a page title prop from my route into my header child component. 我想将页面标题道具从路线传递到标头子组件中。 Here are my routes: 这是我的路线:

var sampleApp= React.createClass({

render: function (){
    return (
        <div className="our-schools-app">
            <Header />
            <RouteHandler />
            <Footer />
        </div>
    );
},

});

var routes = (
<Route name="app" path="/" handler={OurSchoolsApp}>
    <DefaultRoute name="home" handler={HomePage}  />
    <Route name="add-school" handler={AddSchoolPage}  />
    <Route name="calendar" handler={CalendarPage}  />
    <Route name="calendar-detail" path="calendar-detail/:id" handler={CalendarDetailPage} />
    <Route name="info-detail" path="info-detail/:id" handler={InfoDetailPage} />
    <Route name="info" handler={InfoPage} />
    <Route name="news" handler={NewsListPage} />
    <Route name="news-detail" path="news-detail/:id" handler={NewsDetailPage} />
    <Route name="contacts" handler={ContactPage} />
    <Route name="contact-detail" handler={ContactDetailPage} />
    <Route name="settings" handler={SettingsPage} />
</Route>
);


Router.run(routes, function(Handler){
var mountNode = document.getElementById('app');
React.render(<Handler /> , mountNode);
});

I would like to pass the title of my page in the route as a prop like this: 我想像这样通过prop传递路线中页面的标题:

<Route name="info" handler={InfoPage} pageTitle="Info Page" />

To my header component: 对于我的标头组件:

var Header = React.createClass({

render: function () {
    return (
            <div className="bar bar-header"
                style={this.getStyle()}>
                 <HomeButton />
                 <div className="h1 title">{this.props.pageTitle}</div>
            </div>
    )
}
});

But the props show as empty, can anyone help? 但是道具显示为空,有人可以帮忙吗?

I just had the same problem and found this answer in the React Router GitHub issues. 我只是遇到了同样的问题,并在React Router GitHub问题中找到了这个答案

It suggests to pass the props to the Route component and access them from the component with this.props.route.* . 它建议将props传递到Route组件,并使用this.props.route.*从组件访问它们。

I just used if for my own application and find it very easy to use. 我只是将if用于我自己的应用程序,发现它非常易于使用。 ( Especially easier than the accepted answer ) This is the example from the answer (thanks to anjdreas ): 比接受的答案特别容易 )这是答案中的示例(感谢anjdreas ):

Routing: 路由:

React.render((
  <Router>
    <Route path="/" component={RootComp} myprop="foo">
        <Route path="foo" component={FooComp}>
           <Route path="bar" component={BarComp} />
        </Route>
    </Route>
  </Router>
), document.getElementById('example'));

Component: 零件:

class RootComp extends React.Component {
    render: {
        this.props.route.myprop // == "foo"
    }
}

You can't currently (pre 1.0) do that with React Router. 您目前(1.0之前)无法使用React Router做到这一点。 I believe one recommended way is to have wrapper components: 我相信一种推荐的方法是使用包装器组件:

var BaseComponent = React.createClass({
     render: function() {
        return <p>{this.props.text}</p>
     }
});

var FancyComponent = React.createClass({
     return <BaseComponent text="fancy!" />
});

var EvenFancierComponent = React.createClass({
     return <BaseComponent text="SUPER fancy!" />
});

Then these routes: 然后这些路线:

<Route name="standard" handler={BaseComponent} />
<Route name="fancy" handler={FancyComponent} />
<Route name="superfancy" handler={EvenFancierComponent} />

However there's a discussion on this topic on the React Router github issues which provides good news: 但是在React Router github问题上有关于此主题的讨论,这提供了一个好消息:

I just wanted to note here that in the new API (see #1158 ) routes are just plain objects, so you can put whatever props you want on them. 我只是想在这里指出,在新的API(请参阅#1158 )中,路由只是普通对象,因此您可以在其上放置任何所需的道具。 Your transition hooks will get a route arg and components will receive this.props.route, so you should be good to go. 您的过渡钩子将获得路线arg,组件将收到this.props.route,因此您应该一切顺利。

It looks like that new 1.0 release is in beta and so should be coming fairly soon! 好像新的1.0版本处于beta中,因此应该很快推出!

If you are using ES6, then it is also rather nice to inline a stateless functional component using ES6 arrow syntax ... 如果您使用的是ES6,那么使用ES6箭头语法内联无状态功能组件也很不错。

<Route name="info" handler={()=> <InfoPage pageTitle="Info Page" />} />

If your handler component cares about routed params or some other aspect of the route then be sure to pass down the props . 如果您的处理程序组件在乎路由params或路由的其他方面,请确保传递props

<Route name="info" handler={(props)=> <InfoPage pageTitle="Info Page" {...props} /> } />

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

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