Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

报Router must have only one children.错误 #102

Closed
Pentium286 opened this issue Jan 4, 2019 · 3 comments
Closed

报Router must have only one children.错误 #102

Pentium286 opened this issue Jan 4, 2019 · 3 comments

Comments

@Pentium286
Copy link

用的是create-react-app脚手架

router.config.js

import App from '../App';
import App1 from '../App.1';
import App2 from '../App.2';

let routes = [
    { path: '/', component: App, title: '首页' },
    { path: '/App1/', component: App1, title: '页面二' },
    { path: '/App2/', component: App2, title: '页面三' },
];

export default routes;

App.js

import React, { Component } from 'react';
import { Layout, Menu, Icon } from 'antd';
import { HashRouter, Route, Link } from 'react-keeper';
import pageRoutes from './config/router.config';
import './App.scss';

const { Header, Content, Sider } = Layout;

// const routes = [
//   {
//     path: "/sandwiches",
//     component: Sandwiches
//   },
//   {
//     path: "/tacos",
//     component: Tacos,
//     routes: [
//       {
//         path: "/tacos/bus",
//         component: Bus
//       },
//       {
//         path: "/tacos/cart",
//         component: Cart
//       }
//     ]
//   }
// ];

function handleClick(e) {
  console.log('click', e);
}

function RouteWithSubRoutes(route) {
  return (
    <Route
      cache
      path={route.path}
      render={props => (
        <route.component {...props} routes={route.routes} />
      )}
    />
  )
}

class App extends Component {
  state = {
    collapsed: false,
  };

  toggle = () => {
    this.setState({
      collapsed: !this.state.collapsed,
    });
  }
  render() {
    return (
      <Layout>
        <HashRouter>
          <Sider
            trigger={null}
            collapsible
            collapsed={this.state.collapsed}
          >
            <div className="logo" />
            <Menu
              theme="dark"
              mode="inline"
              defaultSelectedKeys={['0']}
              onClick={handleClick}
            >
              {pageRoutes.map((item, index) => (
                <Menu.Item key={index}>
                  <Icon type="user" />
                  <span><Link to={item.path}>{item.title}</Link></span>
                </Menu.Item>
              ))}
            </Menu>
          </Sider>
          <Layout>
            <Header style={{ background: '#fff', padding: 0 }}>
              <Icon
                className="trigger"
                type={this.state.collapsed ? 'menu-unfold' : 'menu-fold'}
                onClick={this.toggle}
              />
            </Header>
            <Content style={{
              margin: '24px 16px', padding: 24, background: '#fff', minHeight: 280,
            }}
            >
              {pageRoutes.map((item, index) => (
                <RouteWithSubRoutes key={index} {...item} />
              ))}
            </Content>
          </Layout>
        </HashRouter>
      </Layout>
    );
  }
};

export default App;

这样配置以后报 Router must have only one children. 错误

@lanistor
Copy link
Owner

lanistor commented Jan 4, 2019

Router(HashRouter)是一个空组件,使用它内部的第一个子元素来render,把HashRouter里面的内容包裹在一个<div>里面就可以了。

@Pentium286
Copy link
Author

Pentium286 commented Jan 4, 2019

路由器(HashRouter)是一个空组件,使用它内部的第一个子元素来渲染,把HashRouter的里面包裹内容在一个<div>里面就可以了。

path={route.path}问题,这里循环怎么操作?


function RouteWithSubRoutes(route) {
  console.log('route: ', route);
  return (
    <Route
      cache
      path={route.path}
      render={props => (
        <route.component {...props} routes={route.routes} />
      )}
    />
  )
}

现在出现了 When Routecomponent has no component property, it's children must be a single tag (not an array), likediv|view . 报这个错

@Pentium286
Copy link
Author

解决了,这样循环

{pageRoutes.map((item, index) => (
                  <Route key={index} cache component={item.component} path={item.path} />
                ))}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants