简体   繁体   English

使用 reactjs 和 react-router,我可以显示组件或 {this.props.children} 吗?

[英]With reactjs and react-router, could I display either a component or {this.props.children}?

Here is the scenario: I have a bunch of components that I allow certain words to be clicked, which will link to a reference page/component.这是场景:我有一堆组件,我允许单击某些单词,这些组件将链接到参考页面/组件。

If the highlighted words are not clicked, I simply display the component (which mind you, there are many of them that allow this mechanism and a whole menu of them on the side).如果未单击突出显示的单词,我只显示该组件(请注意,其中有许多允许这种机制,并且在侧面提供了它们的整个菜单)。

Here is the current code:这是当前的代码:

//app.js
ReactDOM.render(
  <Router history={hashHistory}>
    <Route path="/" component={App} handler={App}>
        <IndexRoute component={Index}/>
         <Route path="help" component={HelpPage}>
            <Route path="/help/reference" component={ReferenceComponent}></Route>
        </Route>

    </Route>
  </Router>,
  destination
);

//help.js:
export default class HelpPage extends Component {
    render() {
        return (
            <Grid>
                <Title/>
               <Row className="show-grid">
                   <Col lg={2} md={4} sm={4}>
                        <HelpMenu
                            getBtnId={this.getBtnId}
                        />
                   </Col>
                   <Col lg={10} md={8} sm={8}>
//part that most matters (currently doesnt work)
                       {<HelpPanels panelIndex={this.state.panelIndex}/> ||
                       this.props.children}
                   </Col>

               </Row>
           </Grid>

        )
    }
}

I commented right above the part that most matters when rendering my HelpPage component.我在渲染我的 HelpPage 组件时最重要的部分的正上方进行了评论。

I understand that what I am trying to do currently doesn't work.我知道我目前正在尝试做的事情不起作用。 What I want is basically either you post one of the page's components, or if you are in the .../help/reference route you post that Reference component.我想要的基本上是您发布页面的组件之一,或者如果您在 .../help/reference 路线中,则发布该参考组件。 Is there any way I can do this?有什么办法可以做到这一点吗? I've messed around with it a bit, but so far, nothing.我有点搞砸了,但到目前为止,什么都没有。

Just change your route config so that according to the selected index you can show HelpPanels as child route under HelpPage and for reference route you show ReferenceComponent .只要改变你的路线配置,这样根据选择的指数可以显示HelpPanels下为孩子航线HelpPage和参考路线告诉你ReferenceComponent Since reference route is a child route of help route you don't need to explicitly write full path as /help/reference in the config object.由于参考路线是帮助路线的子路线,因此您无需在配置对象中将完整路径明确写为 /help/reference 。

ReactDOM.render(
  <Router history={hashHistory}>
    <Route path="/" component={App} handler={App}>
        <IndexRoute component={Index}/>
         <Route path="help" component={HelpPage}>
            <Route path="details/:helpId" component={HelpPanels}/>
            <Route path="reference" component={ReferenceComponent}></Route>
        </Route>

    </Route>
  </Router>,
  destination
);

Then just display {this.props.children} under HelpPage Component.然后只在HelpPage组件下显示{this.props.children}

export default class HelpPage extends Component {
    render() {
        return (
            <Grid>
                <Title/>
               <Row className="show-grid">
                   <Col lg={2} md={4} sm={4}>
                        <HelpMenu
                            getBtnId={this.getBtnId}
                        />
                   </Col>
                   <Col lg={10} md={8} sm={8}>
//if the route is /help/reference the children will be `ReferenceComponent` component or if it is something in the format /help/details/3 it will show `HelpPanels` component and this.props.params.helpId will be 3.
                       {this.props.children}
                   </Col>

               </Row>
           </Grid>

        )
    }
}

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

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