我正在使用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"
----&GT;更新:
在搜索确切的问题区域后,我遇到了两个具体问题。
一个。 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的其他方面,例如渲染,根本不起作用......在这里绝望......这是一个错误还是我做错了什么?< / p>
---&GT;更新
这是我的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"
}
}
答案 0 :(得分:0)
我有几乎相同的问题。试试这个:
React.render(React.createElement(Count), document.getElementById('container'));
答案 1 :(得分:0)
您应该安装Reactify 1.1.x:
"reactify": "~1.1.x"