简体   繁体   English

有多个子代的React-router路由

[英]React-router routes with multiple children

One of my routes needs two children to make what I want to do work. 我的一条路线需要两个孩子来做我想做的工作。

The main app page where I define all the routes would probably look something like this: 我在其中定义所有路线的主应用页面可能看起来像这样:

    <Route path="/logs" component={Logs}>
        <Route path="/logs/archive(/**)" component={ArchiveAction}></Route>
        <Route path="/logs/view(/**)" component={LogApp}></Route>
    </Route>

As for the Logs component, if I am at the /logs/view/whatever route, it will render the component called LogApp which will be the whole page. 至于Logs组件,如果我位于/ logs / view / whatever路线,它将呈现名为LogApp的组件,这将是整个页面。

If it's on /logs/archive/whatever a subcomponent of Logs is what changes. 如果在/ logs / archive /上,则Logs的子组件会发生什么变化。 But this part works fine. 但是这部分工作正常。 What I cannot get to work is /logs/view/whatever. 我无法上班的是/ logs / view /无论如何。 Here is my current Logs component: 这是我当前的日志组件:

export class Logs extends Component{

    render(){
        var initialLogsPage =(<Grid>
                <Row>

                    <Col lg={4} md={4} sm={4}>
                        <Directories/>
                    </Col>
                    <Col lg={4} md={4} sm={4}>
                        <Archives children={this.props.children}/>
                    </Col>

                </Row>
            </Grid>);
        return(
            <div>
            {initialLogsPage || this.props.children}
            </div>
        );
    }
}

I understand that my use of this.props.children is probably where the problem lies since I am dealing with two. 我知道我使用this.props.children可能是问题所在,因为我正在处理两个。 Is there any way to get around this? 有什么办法可以解决这个问题?

Just change your routes: 只需更改您的路线:

<Route path="/logs" component={Logs}>
    <Route path="/logs/archive(/**)" component={ArchiveAction}></Route>
</Route>
<Route path="/logs/view(/**)" component={LogApp}></Route>

You can use Route s without paths: 您可以使用不带路径的Route

// Route config
<Route path="/logs" component={Logs}>
  <Route component={Archives}>
    <Route path="/logs/archive(/**)" component={ArchiveAction}></Route>
  </Route>
  <Route path="/logs/view(/**)" component={LogApp}></Route>
</Route>

// components
const Archives = ({ children }) => (
  <Grid>
    <Row>
      <Col lg={4} md={4} sm={4}>
        <Directories/>
      </Col>
      <Col lg={4} md={4} sm={4}>
        <Archives>{children}</Archives>
      </Col>
    </Row>
  </Grid>
);

const Logs = ({ children }) => <div>{children}</div>; // add shared layout here

You should also think about what should be rendered at /logs . 您还应该考虑应该在/logs呈现什么。 Either change Logs to check props.children , eg, <div>{children || <LogIndexComponent />}</div> 更改Logs以检查props.children ,例如<div>{children || <LogIndexComponent />}</div> <div>{children || <LogIndexComponent />}</div> , or include a IndexRoute in your route config. <div>{children || <LogIndexComponent />}</div> ,或在路由配置中包含IndexRoute

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

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