如何使用其他文件中的反应组件

Ban*_*hee 10 javascript asp.net components reactjs react-jsx

我有一个简单的reactJS组件,如下所示:

var LikeCon = React.createClass({

    render: function() {
        return (
            <span>Like</span>
        );
    }
});
Run Code Online (Sandbox Code Playgroud)

它放在名为Common.jsx的文件中.我试图像这样使用antoher jsx文件中的这个LinkeCon组件

var FeedTopic = React.createClass({
        render: function() {
            var test = false;
            return (
                <div className="topic">
                        {LikeCon}
                </div>

            );
        }
});
Run Code Online (Sandbox Code Playgroud)

问题是抛出了这个异常

将"FeedBox"渲染为"react1"时出错:ReferenceError:未定义LikeCon

这是导入在布局页面上的样子

<script src="@Url.Content("~/Scripts/Common.jsx")"></script>
<script src="@Url.Content("~/Scripts/Grid.jsx")"></script>
<script src="@Url.Content("~/Scripts/Feed.jsx")"></script>
Run Code Online (Sandbox Code Playgroud)

我的想法是,如果包含共享组件的Common.jsx是第一个,那么var也可用于其他反应组件?

编辑:

这是放在Layout.cshtml上

<script type="text/jsx" src="@Url.Content("~/Scripts/JSXTransformer.js")"></script>
<script type="text/jsx" src="@Url.Content("~/Scripts/Common.jsx")"></script>
<script type="text/jsx" src="@Url.Content("~/Scripts/Grid.jsx")"></script>
<script type="text/jsx" src="@Url.Content("~/Scripts/Feed.jsx")"></script>
Run Code Online (Sandbox Code Playgroud)

现在引用该组件<LikeCon like="0" />而不是{LikeCon}.

编辑2:

这就是我使用LikeCon的方式

var TopicComments = React.createClass({
    render: function() {
        var comment = this.props.data.map(function(com, i) {
            return (
            <article key={i}>
            <div className="commentCon">
                <div className="tUImgLnk">
                    <a title={com.UserName} target="_blank" href={com.UserInfoUrl}>
                        <img className="tUImg" src={com.UserPicSrc} />
                    </a>
                </div>
                <b><a href="#" title={"Visit " + com.UserName} target="_blank">{com.UserName}</a></b>&nbsp;:&nbsp;
                <span className="content">
                    {com.Message}
                </span>
                <div className="status">
                    <div className="dateCreated dimText">
                        {com.DateCreated}
                    </div>  
                    <LikeCon initialLike={com.Like} initialLikeCount={com.LikeCount} objectId={com.Id} categoryKey={1} userId={this.props.userId} />
                    <article></article>
                </div>
            </div>
            </article>);
        }.bind(this));
        return(
            <div className="comments">
                {comment}
            </div>
            );
    }
});
Run Code Online (Sandbox Code Playgroud)

这是脚本导入的样子

    <script src="http://fb.me/react-0.12.2.js"></script>
    <script src="@Url.Content("~/Scripts/jquery-2.1.3.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.autosize.min.js")"></script>
    <script src="@Url.Content("~/Scripts/spin.min.js")"></script>
    <script src="@Url.Content("~/Scripts/JSXTransformer.js")"></script>

    <script src="@Url.Content("~/Scripts/Grid.jsx")"></script>
    <script src="@Url.Content("~/Scripts/Feed.jsx")"></script>
    @RenderSection("ScriptFoot", required: false)
    @Html.ReactInitJavaScript()
</body>
Run Code Online (Sandbox Code Playgroud)

这是我得到的例外:

将"FeedBox"渲染为"react1"时出错:ReferenceError:在React.createClass.render中没有定义LikeCon(脚本文档[7]:83:33) - > React.createElement(LikeCon,{initialLike:this.props.data .Like,i at Script Document [2]:7021:34 at wrapper(Script Document [2]:12893:21)at Script Document [2]:6563:14 at wrapper(Script Document [2]:12893:21)在ReactMultiChild.Mixin.mountChildren(Script Document [2]:12352:42)
at ReactDOMComponent.Mixin._createContentMarkup(Script Document [2]:7801:32)at Script Document [2]:7723:14 at wrapper(Script Document [ 2]:12893:21)脚本文件[2]:6569:44 at at wrapper(Script Document [2]:12893:21)at Script Document [2]:6569:44 at wrapper(Script Document [2]:12893 :21)at Script Document [2]:13797:38 at Mixin.perform(Script Document [2]:16855:20)at renderToString(Script Document [2]:13795:24)at Script Document [9] [temp] :1:7行:7021专栏:34

Wik*_*lik 2

  1. 添加:<script src="Scripts/JSXTransformer.js"></script>
  2. 而不是{LikeCon}使用<LikeCon/>
  3. type="text/jsx"在您的脚本中使用