简体   繁体   English

ReactJS + Gulp + Reactify + Browserify元素类型无效:应为字符串,但得到:未定义

[英]ReactJS + Gulp + Reactify + Browserify Element type is invalid: expected a string but got: undefined

I am getting this error: 我收到此错误:

bundle.js:26912 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. bundle.js:26912未捕获的错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 You likely forgot to export your component from the file it's defined in. 您可能忘记了从定义文件中导出组件。

Directory Structure... 目录结构...

 . ├── components │   ├── index.js │   ├── NavBar.js │   └── SignIn.js ├── css │   └── style.css ├── index.html └── main.js 

This is my code: main.js 这是我的代码: main.js

 var React = require('react'); var ReactDOM = require('react-dom'); var Router = require('react-router').Router; var Route = require('react-router').Route; var Navbar = require("./components/NavBar").default; var SidePanel = require("./components/index").default; var App = React.createClass({ render : function() { return ( <div className="Main"> <Navbar/> <div className="container"> <SidePanel /> <div className="row"> </div> </div> </div> ) } }); ReactDOM.render( <Router> <Route path="/" components={App}/> </Router>, document.getElementById('app')); 

My gulpfile.js 我的gulpfile.js

 "use strict"; var gulp = require('gulp'); var connect = require('gulp-connect'); //Runs a local dev server var open = require('gulp-open'); //Open a URL in a web browser var browserify = require('browserify'); // Bundles JS var reactify = require('reactify'); // Transforms React JSX to JS var source = require('vinyl-source-stream'); // Use conventional text streams with Gulp var concat = require('gulp-concat'); //Concatenates files // var lint = require('gulp-eslint'); //Lint JS files, including JSX var babelify = require('babelify'); // support for es6 var config = { port: 9005, devBaseUrl: 'http://localhost', paths: { html: './src/*.html', js: './src/**/*.js', images: './src/images/*', css: [ 'node_modules/bootstrap/dist/css/bootstrap.min.css', 'node_modules/bootstrap/dist/css/bootstrap-theme.min.css', './src/css/*.css' ], dist: './dist', mainJs: './src/main.js' } } //Start a local development server gulp.task('connect', function() { connect.server({ root: ['dist'], port: config.port, base: config.devBaseUrl, livereload: true }); }); gulp.task('open', ['connect'], function() { gulp.src('dist/index.html') .pipe(open('', { url: config.devBaseUrl + ':' + config.port + '/'})); }); gulp.task('html', function() { gulp.src(config.paths.html) .pipe(gulp.dest(config.paths.dist)) .pipe(connect.reload()); }); gulp.task('js', function() { browserify(config.paths.mainJs) //.transform(babelify) .transform(reactify) .bundle() .on('error', console.error.bind(console)) .pipe(source('bundle.js')) .pipe(gulp.dest(config.paths.dist + '/scripts')) .pipe(connect.reload()); }); gulp.task('css', function() { gulp.src(config.paths.css) .pipe(concat('bundle.css')) .pipe(gulp.dest(config.paths.dist + '/css')); }); // Migrates images to dist folder // Note that I could even optimize my images here gulp.task('images', function () { gulp.src(config.paths.images) .pipe(gulp.dest(config.paths.dist + '/images')) .pipe(connect.reload()); //publish favicon gulp.src('./src/favicon.ico') .pipe(gulp.dest(config.paths.dist)); }); // gulp.task('lint', function() { // return gulp.src(config.paths.js) // .pipe(lint({config: 'eslint.config.json'})) // .pipe(lint.format()); // }); gulp.task('watch', function() { gulp.watch(config.paths.html, ['html']); gulp.watch(config.paths.js, ['js']); }); gulp.task('default', ['html', 'js', 'css', 'images', 'open', 'watch']); 

NavBar.js NavBar.js

 var React = require('react'); var Navbar = React.createClass({ render: function() { return ( <div> <nav className="navbar navbar-default"> <div className="container-fluid"> <ul className="nav navbar-nav"> <li><a href="/">Home</a></li> <li><a href="/#authors">Authors</a></li> <li><a href="/#about">About</a></li> </ul> </div> </nav> </div> ); } }); module.exports = Navbar; 

index.js index.js

 var React = require("react"); var SignInForm = require("./SignIn"); var SidePanel = React.createClass({ render: function() { return ( <div className="side-panel"> <div className="col-md-4"> <SignInForm /> </div> </div> ) } }); module.exports = SidePanel; 

Using ES5 syntax 使用ES5语法

您已在Route ..中编写了components而不是component

Replace your main.js file with this - 以此替换您的main.js文件-

var React = require('react');
var ReactDOM = require('react-dom');
import {Router, Route, browserHistory} from 'react-router';
var Navbar = require("./components/NavBar");
var SidePanel = require("./components/index");

var App = React.createClass({
    render : function() {
        return (
            <div className="Main">
                <Navbar/>
                    <div className="container">
                        <SidePanel />
                            <div className="row">
                            </div>
                    </div>
            </div>
        )
    }
});
ReactDOM.render((<Router history={browserHistory}><Route path="/" components={App}/></Router>),document.getElementById('app'));

暂无
暂无

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

相关问题 元素类型无效:应为字符串(用于内置组件)或类/函数(用于复合组件)但得到:reactjs 中未定义 - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined in reactjs 元素类型无效:应为字符串或类/函数,但得到:未定义 - Element type is invalid: expected a string or a class/function but got: undefined React Native Element类型无效,预期字符串但未定义 - React Native Element type is invalid expected a string but got undefined 元素类型无效:期望的字符串或类但未定义 - Element type is invalid: expected string or class but got undefined 吞并浏览器化,重新认识和重新认识? - Gulp with browserify, tsify, and reactify? 元素类型无效:应为字符串 (...) 但得到:对象 - Element type is invalid: expected a string (...) but got: object 更新我得到的所有依赖项:元素类型无效:预期为字符串或类/函数(对于复合组件)但得到:未定义 - Updating all dependencies I got: Element type is invalid: expected a string or a class/function (for composite components) but got: undefined 错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:ReactJS 中的对象 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS 元素类型无效,预期字符串未定义 - Element type is invalid expected a string undefined React Native - “元素类型无效:需要字符串或类/函数但未定义” - 导入自定义库? - React Native - “Element type is invalid: expected a string or a class/function but got undefined” - importing custom library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM