类型错误:未定义不是对象(评估“this.__reactAutoBindMap”)

IT技术 javascript node.js reactjs react-jsx
2021-05-25 11:29:12

我正在使用 NPM 安装的最新版本的 react.js。我已经编写了这段代码,当我将它放入 jsfiddle 时它会起作用,但当我在自己的设置中尝试时却不会。这是我正在使用的代码:

/** @jsx React.DOM */

var React = require('react');

var MyButton = React.createClass({
    render: function(){
        return ( <button onClick={this.props.onClick} >more!</button> );
    }
});

var Count = React.createClass({

    getInitialState: function(){
        return { counter: 0 };
    },

    increment: function(){ 
        this.setState({ 
            counter: this.state.counter + 1
        });
    },

    render: function(){
        return ( <div>
           <li>{this.state.counter}</li>
           <MyButton onClick={this.increment} />
        </div> );
    }
});

React.render( <Count />, document.getElementById('container'));

然后我的 HTML 文件如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>what the f</title>
</head>
<body>
<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>
<script src="js/main.js"></script>
</body>
</html>

在我的浏览器中,我收到一条错误消息:

"TypeError: undefined is not an object (evaluating 'this.__reactAutoBindMap')
(anonymous function)"

和警告说:

"Warning: Something is calling a React component directly. Use a factory or JSX instead. See: http://fb.me/react-legacyfactory"

----> 更新:

在搜索确切的问题区域后,我遇到了两个具体问题。

A. 该React.render()函数不接受 JSX。

为了使任何内容都无误地显示,我必须使用: React.render(React.createElement(Count), document.getElementById('container'));

代替: React.render( <Count />, document.getElementById('container'));

B. 然后我每次尝试访问对象属性时都会收到错误消息,例如,如果在上面的代码中我注释掉了其中的任何内容,this.something则代码执行得很好,否则会出现错误:

TypeError: undefined is not an object (evaluating 'this.__reactAutoBindMap')

这两个问题似乎都与 jsx 的问题有关,但我不确定为什么 jsx 会在某些方面起作用,而在其他方面不起作用。我可以<h1>hello!</h1>毫无意外地返回,但是 jsx 的其他方面,例如在渲染中,根本不起作用......在这里变得绝望......这是一个错误还是我做错了什么?

---> 更新

这是我的 gulp 文件:

var connect = require('gulp-connect');
var gulp = require('gulp');
var source = require('vinyl-source-stream'); // Used to stream bundle for further handling
var browserify = require('browserify');
var watchify = require('watchify');
var reactify = require('reactify'); 
var concat = require('gulp-concat');

gulp.task('browserify', function() {
    var bundler = browserify({
        entries: ['app_root/js/main.js'], 
        transform: [reactify], // convert JSX to javascript
        debug: true,
        cache: {}, packageCache: {}, fullPaths: true 
    });
    var watcher  = watchify(bundler);

    return watcher
    .on('update', function () { 
        var updateStart = Date.now();
        console.log('Updating!');
        watcher.bundle() // Create new bundle that uses the cache for high performance
        .pipe(source('app_root/js/main.js'))
        .pipe(gulp.dest('dist/js'));
        console.log('Updated!', (Date.now() - updateStart) + 'ms');
    })
    .bundle()
    .pipe(source('app_root/js/main.js'))
    .pipe(gulp.dest('dist/js'));
});
// concat app to directory being served
gulp.task('conkat', function(){
    gulp.src('/src/dist/app_root/js/main.js')
      .pipe(concat('main.js'))
      .pipe(gulp.dest('dist/js'));
});

// copy index.html to served directory
gulp.task('copy', function(){
    gulp.src('app_root/index.html')
      .pipe(gulp.dest('dist'));
          gulp.src('/src/dist/app_root/js/main.js')
});

// watch app directory
gulp.task('watch', function(){
    gulp.watch('app_root/**/*.*', ['reload']);
});

// serve the dist directory
gulp.task('serveDist', function(){
    connect.server({
        root: 'dist'
    });
});

// run on change
gulp.task('reload', [ 'browserify','conkat', 'copy' ]);

// run all
gulp.task('default', [ 'browserify', 'conkat','copy', 'serveDist', 'watch' ]);

这是我的 package.son:

{
    "private": true,
    "devDependencies":
    {
        "gulp":"^3.8.8",
        "browserify":"^9.0.6",
        "gulp-concat":"^2.4.1",
        "react":"^0.13.1",
        "reactify":"^0.14.0",
        "watchify":"^3.1.0",
        "vinyl-source-stream":"^1.1.0",
        "react-router":"^0.13.2",
        "gulp-connect":"^2.2.0"
    }
}
2个回答

我有几乎同样的问题。尝试使用这个:

 React.render(React.createElement(Count), document.getElementById('container'));

你应该安装 Reactify 1.1.x:

"reactify": "~1.1.x"