简体   繁体   English

使用React Router v4是否可以在子组件中创建多级路由?

[英]Using React Router v4 is it possible to create multi level routes in child components?

So I'm fairly new to the React framework, and I have decided to use Router v4, even though it is in beta, at the time of writing this, it seems to be the best way to go about things. 所以我对React框架还是比较陌生的,我决定使用Router v4,即使它处于测试阶段,在撰写本文时,它似乎是最好的方法。

I have a router that shows one of two components 我有一个显示两个组件之一的路由器

import React from 'react';
import { HashRouter as Router, Route } from 'react-router-dom';
import Category from './category/Category';
import Hist from './hist/Hist';

const Controls = React.createClass({
  render: function() {
    return (
      <Router>
        <div>
          <Route path='/category/:categoryId' component={Category}} />
          <Route path='/Hist' component={Hist} />
        </div>
      </Router>
    );
  }
});

export default Controls;

This works like a charm, of course, what I want to do now is to have some more routing inside of the Category component. 当然,这就像一个魅力,我现在要做的是在Category组件中有更多的路由。 I have tried something like this: 我试过这样的事情:

import React from 'react';
import { Route, Link } from 'react-router-dom';

const Category = React.createClass({
  render: function() {
    return (
      <div>
        <ul>
          <li><Link to='/category/1'>Category 1</Link></li>
          <li><Link to='/category/2'>Category 2</Link></li>
        </ul>
        <div className='category-display'>
          <Route path='/category/1'>This is Category 1</Route>
          <Route path='/category/2'>This is Category 2</Route>
        </div>
      </div>
    );
  }
});

But this is giving me a weird error saying something like 但是这给了我一个奇怪的错误,说了些什么

Uncaught Error: React.Children.only expected to receive a single React element child.

Is it even possible to pull off a stunt like this or does anyone have any other good suggestions for me? 甚至有可能像这样做一个特技表演,还是有人对我有任何其他好的建议?

Or is the best solution to rethink and implement some sort of conditional rendering depending on the route I get to the Category component as props from the first router. 或者是重新思考和实现某种条件渲染的最佳解决方案,具体取决于我从第一个路由器获取到类别组件的路径。


Update: 更新:

I realised that <Route> does not seem to accept contents, so I added a component called Display and imported it like so: 我意识到<Route>似乎不接受内容,所以我添加了一个名为Display的组件并将其导入如下:

import React from 'react';
import { Route, Link } from 'react-router-dom';
import Display

const Category = React.createClass({
  render: function() {
    return (
      <div>
        <ul>
          <li><Link to='/category/1'>Category 1</Link></li>
          <li><Link to='/category/2'>Category 2</Link></li>
        </ul>
        <div className='category-display'>
          <Route path='/category/1' component={Display} />
          <Route path='/category/2' component={Display} />
        </div>
      </div>
    );
  }
});

However Display is not rendered. 但是不显示显示。 Any ideas as to why this is would be greatly appreciated. 任何关于为什么会这样的想法将不胜感激。

So it actually turns out that my update (see question above) did the trick. 所以事实证明我的更新(见上面的问题)就是这样做的。 The Display component was at fault here, not the router. 显示组件在此处出现故障,而不是路由器。

So to recap if anyone else stumbles upon this: 因此,如果其他人偶然发现这一点,请回顾一下:

<Route> does not accept any contents, one could have used the render function of <Route> to do some quick testing. <Route>不接受任何内容,可以使用<Route>render功能进行一些快速测试。 Everything is neatly summarised in the React Router API documentation . React Router API文档中对所有内容进行了整齐的总结。

Cheers! 干杯!

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

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