【问题标题】:React.js Cannot read property '__reactAutoBindMap' of nullReact.js 无法读取 null 的属性“__reactAutoBindMap”
【发布时间】:2015-07-01 15:05:00
【问题描述】:

我知道以前有人问过这个问题,但建议的解决方案对我没有帮助,所以,这是我的代码,由于某种原因失败了。

app.js:

(function(React, _) {
    var App = require('./components/App.react');

_.mixin(_.string.exports());

var render = function() {
    React.render(
        React.createElement(App),
        document.getElementById('reactContent')
    );
};
render();
}(React, _));

然后,App.react.js:

var React = require('react');
var Test = React.createClass({
render: function() {
    return <div>Hello world</div>
}
})

module.exports = Test;

现在,我的控制台说:

Warning: This JSX uses a plain function. Only React components are valid in     React's JSX transform.
app.js:20281 Warning: Something is calling a React component directly. Use a     factory or JSX instead. See: http://fb.me/react-legacyfactory
app.js:6475 Uncaught TypeError: Cannot read property '__reactAutoBindMap' of null

感觉是最简单的事情......我将包含我的 package.json 以防万一。

{
  "name": "app",
  "version": "0.0.1",
  "devDependencies": {
"gulp": "3.8.10",
"gulp-run": "1.6.5",
"gulp-less": "2.0.1",
"gulp-concat": "2.4.3",
"gulp-uglify": "1.0.2",
"gulp-minify-css": "0.3.11",
"gulp-jshint": "1.9.0",
"browser-sync": "1.8.2",
"browserify": "3.44.2",
"vinyl-source-stream": "1.0.0",
"vinyl-buffer": "1.0.0",
"reactify": "latest",
"underscore": "^1.7.0",
"del": "1.1.1",
"envify": "~3.0.0"
  },
  "paths": {
"less": "assets/less/*.less",
"js": "./app/**/*.js",
"jsx": "./app/**/*.jsx",
"app": "./app/app.js",
"html": "*.html"
  },
  "dest": {
    "style": "style.css",
    "app": "app.js",
    "dist": "dist"
  },
  "dependencies": {
"react": "latest",
"underscore": "^1.7.0",
"flux": "^2.0.0"
  }
}

您可以从 .json 文件中看到这里还有很多内容,我只是重新构建了代码以创建一个非常简单的案例并隔离问题。

有什么想法吗?

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    我认为您需要React.createFactory(require('./components/App.react')),如下所述:https://gist.github.com/sebmarkbage/d7bce729f38730399d28

    或使用 ES6 类:https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html

    编辑: 我找到了原博文:https://facebook.github.io/react/blog/2014/10/14/introducing-react-elements.html

    【讨论】:

    • 谢谢,但快速浏览后我发现如果我使用的是 JSX,这不会影响我,我相信我是,对吧?无论如何 - 将“var React = require('react')”放在 app.js 的顶部后,它工作正常。
    • 我认为它确实如此,因为在 react.Render 你没有使用 jsx ...哦,当它工作时它工作:-)
    • 我最终使用了App = React.createFactory(require('path/to/App'))
    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2021-07-20
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多