javascript - Facebook 的 react.js——对象不是函数

标签 javascript facebook markdown frontend reactjs

继续 Facebook 的 read.js tutorial ,我得到这个错误:

Uncaught TypeError: Property 'CommentList' of object [object Object] is not a function

其实react.js自己的examples page有:

Uncaught TypeError: object is not a function

谁能解释一下正确的用法?


教程进度

导入以下两个javascripts:

http://fb.me/react-0.4.1.js http://fb.me/JSXTransformer-0.4.1.js

HTML 是一行:

  <div id="content"></div>

还有 javascript 或者更确切地说 <script type="text/jsx">看起来像这样:

var CommentBox = React.createClass({
    render: function() {
        return (
           <div class="commentBox">
        <h1>Comments</h1>
        <CommentList />
        <CommentForm />
        </div>
        );
    }
});


React.renderComponent(
    <CommentBox />,
    document.getElementById('content')
);


var CommentList = React.createClass({
    render: function() {
        return (
        <div class="commentList">
        <Comment author="Pete Hunt">This is one comment</Comment>
        <Comment author="Jordan Walke">This is *another* comment</Comment>
        </div>
    );
    }
});

最佳答案

这里有两个主要问题。

首先,当 React.renderComponent 被调用时, CommentList 还没有被分配,因此仍然是未定义的。这是导致错误的原因,因为 CommentBox 的渲染函数引用了

<CommentList />

jsx编译成什么

CommentList(null)

当执行此操作且 CommentList 未定义时,我们会收到错误消息,因为未定义不是函数。要解决这个问题,我们需要做的就是在调用 React.renderComponent 之前移动 CommentList 声明。

其次,Comment 和 CommentForm 没有在任何地方定义。我们需要删除对它们的引用,或者从教程中引入它们的声明。

作为引用,这是原始代码的 jsfiddle:http://jsfiddle.net/jacktoole1/nHTr4/

如果我们包含 Comment 的声明但只是删除对 CommentForm 的引用,则修复后的代码如下所示:http://jsfiddle.net/jacktoole1/VP5pa/

关于javascript - Facebook 的 react.js——对象不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18771166/

相关文章:

javascript - 为什么我的图像在使用 anchor 标记时变得不对齐?

javascript - 在 JavaScript 中用前导零填充数字

javascript - 如何使用 JavaScript 重新填充 Facebook 共享对话框

css - Facebook Like 按钮发布到 Facebook Box 显示不正确?

用于添加外部链接的 Vim 插件

javascript - 为什么导入类会使此代码失败?

javascript - ionic Angularjs vs jquery : $http. jsonp() vs jquery.ajax

facebook - 不使用 "publish_actions"发布照片的 Facebook API 替代方案

bash - 使用 Markdown.pl 批量转换 Markdown 文件

python-3.x - 关闭 Jupyter Notebook 输出中的缩写?