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

情到浓时终转凉″ 提交于 2020-01-15 12:07:39

问题


I am working with the latest version of react.js installed by NPM. I've written this code which works when I put it through jsfiddle, but not when I try it in my own setup. Here is the code I'm working with:

/** @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'));

and then my HTML file looks like this:

<!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>

In my browser I get an error saying:

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

and a warning that says:

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

----> update:

There are two specific issues I am experiencing after searching for exact problem areas.

A. The React.render() function is not accepting JSX.

In order for anything to display without error I have to use: React.render(React.createElement(Count), document.getElementById('container'));

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

B. Then I get an error anytime I try to access the objects properties, for example, if in the above code I comment out anything that has this.something in it then the code executes just fine, otherwise it gives the error:

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

both of these problems seem like they could be related to a problem with jsx, but I'm not sure why jsx would work in some ways but not others. I am able to return <h1>hello!</h1> without incident, but other aspects of jsx, in render for example, do not work at all... getting desperate here... is this a bug or am I doing something wrong?

---> update

here is my gulp file:

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' ]);

and heres my 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"
    }
}

回答1:


I have nearly the same problem. Try use this:

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



回答2:


You should install Reactify 1.1.x:

"reactify": "~1.1.x"


来源:https://stackoverflow.com/questions/29424470/typeerror-undefined-is-not-an-object-evaluating-this-reactautobindmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!