代码之家  ›  专栏  ›  技术社区  ›  Foysal94

ES6+反应,错误:反应。createElement:type不应为null、undefined、boolean或number

  •  0
  • Foysal94  · 技术社区  · 10 年前

    使用ES6学习ReactJS。尝试实现React Bootstrap组件。我也在使用反应路由器。我正在尝试实现导航栏组件。

    它只是一个拥有品牌和搜索框的搜索引擎。我的目标是更多地扩展和使用搜索框组件,所以我将其放在下面单独的组件中。

    位置搜索框.js

    import React, {PropTypes} from 'react'
    import Form, {FormGroup, FormControl} from 'react-bootstrap'
    
    export default function LocationSearchBox(props) {
        return (
            <FormGroup>
                <FormControl type="text" placeholder="Search" />
                <Button bsStyle="success" type="submit">Submit</Button>
            </FormGroup>
        )
    }
    

    导航器将出现在所有页面上,所以我将它放在最上面的路线上,它的组件是Main。下面显示的js文件以及路由。js来管理客户端路由。

    import React, {Component} from 'react';
    import {Navbar, NavbarHeader, NavbarBrand, NavbarCollapse} from 'react-bootstrap';
    import {default as Search} from './LocationSearchBox'
    
    export default class Main extends Component{
    
        constructor(props) {
            super(props);
            this.props = props;
        }
    
        render() {
            return(
                <Navbar>
                    <NavbarHeader>
                        <NavbarBrand>
                            <a href="#">React-Bootstrap</a>
                        </NavbarBrand>
                    </NavbarHeader>
                    <NavbarCollapse>
                        <Search/>
                    </NavbarCollapse>
                </Navbar>
    
            )
        }
    
    }
    

    路由.js

    import React from 'react';
    import ReactRouter, {Router, Route, IndexRoute, browserHistory} from 'react-router';
    import Main from '../components/Main';
    
    export var routes = (
        <Router history={browserHistory}>
            <Route path='/' component={Main} />
    
        </Router>
    );
    

    指数js在下面,是Webpack/babel中使用的入口文件

    import React from 'react';
    import ReactDOM from 'react-dom';
    import {routes} from './config/routes';
    
    ReactDOM.render(routes, document.getElementById('root'));
    

    因此,当我运行webpack-dev服务器时,转到localhost:8080作为主路由应该到达的默认端口。我相信是这样的,但我得到了错误,即3个同类错误。

    Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of `Main`.
    

    我认为这可能是由于我在Main中导入导航栏组件的方式所致。js,例如NavbarHeader、NavbarCollapse等,但这只是猜测。我真的很感激你能帮我解决这个问题,谢谢。

    1 回复  |  直到 10 年前
        1
  •  1
  •   TimoStaudinger    10 年前

    你是 使用 Button 不导入组件 它:

    <Button bsStyle="success" type="submit">Submit</Button>