【问题标题】:ReactJS.net unable to debugReactJS.net 无法调试
【发布时间】:2015-09-14 02:42:41
【问题描述】:

我使用 ReactJS.Net 和 ASP.NET 5 创建了一个小型应用程序。如果我使用 @Html.React 渲染组件服务器端并将其绑定到 MVC

@using System.Threading.Tasks
@using React.AspNet
@model Models.DashboardViewModel

@Html.React("MessageBoard", new
{
    recentPosts = Model.RecentPosts
})

messageBoard.jsx

"use strict";

var MessageBoard = React.createClass({
    render: function(){
        return (<table className="table forum table-striped">
                <thead> 
                    <tr>
                        <th className="cell-stat"></th>
                        <th>Topic</th>
                        <th className="cell-stat text-center hidden-xs hidden-sm">Replies</th>
                        <th className="cell-stat-2x hidden-xs hidden-sm">Last Post</th>
                    </tr>
                </thead>
                    <tbody>
                        {this.props.recentPosts.map(function(boardPost){
                            return <BoardPostRow post={boardPost}/>;
                        }, this)}
                    </tbody>
                </table>)
        }
});

这一切都很好。问题是当我去源代码时,没有 .js 文件所以我无法调试。对于一些简单的只读元素,这可能没问题。但是现在我想渲染一些包含状态的交互元素,一种用于在“留言板”上创建新帖子的表单。这是同一个 *.cshtml 文件中的代码。

<div id="newPost"></div>

@section scripts
{
    <script src="@Url.Content("~/scripts/Components/Board/boardPostForm.jsx")"></script>
    <script src="@Url.Content("~/scripts/Components/Common/textInput.jsx")"></script>
    <script>React.render(BoardPostForm({}), document.getElementById("newPost"))</script>
    @Html.ReactInitJavaScript()
} 

我在控制台中得到的错误是:

Uncaught SyntaxError: Unexpected token <
textInput.jsx:19 Uncaught SyntaxError: Unexpected token <
(index):69 Uncaught ReferenceError: BoardPostForm is not defined(anonymous function) @ (index):69
(index):70 [.NET] Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of MessageBoard. See http://fb.me/react-warning-keys for more information.
(index):71 [.NET] Warning: Unknown DOM property class. Did you mean className?
(index):71 Uncaught ReferenceError: MessageBoard is not defined

尝试读取 .jsx 似乎出错,因为当我单击错误时它会将我带到渲染函数。我在这里想念什么?也许我需要在构建过程中进行 jsx->js 转换(我正在使用 Gulp),而不是依赖 ReactJS.NET?

【问题讨论】:

    标签: reactjs asp.net-core react-jsx reactjs.net


    【解决方案1】:

    如果您在网络浏览器中点击/scripts/Components/Board/boardPostForm.jsx 会发生什么?它应该显示已编译的 JSX,并在文件顶部有一些 ReactJS.NET 信息。如果没有,请确保在您的Web.config 中配置了*.jsx 处理程序。它应该看起来像这样:

    <add name="ReactJsx" verb="GET" path="*.jsx" type="React.Web.JsxHandlerFactory, React.Web" preCondition="integratedMode" />
    

    我在这里缺少什么?也许我需要在构建过程中进行 jsx->js 转换(我正在使用 Gulp),而不是依赖 ReactJS.NET?

    您可以根据自己的喜好使用 Gulp。我这里有一个示例,它使用 Webpack 进行捆绑,使用 ReactJS.NET 进行服务器端渲染:https://github.com/reactjs/React.NET/tree/master/src/React.Sample.Webpack

    【讨论】:

    • 我忘了说我使用的是 ASP.NET 5,所以没有网络配置。如果我转到 .jsx 文件,我看到的是未编译格式。感谢您的快速回复,我会看看您的样品。
    • 我现在正在编译为 .js 作为我的构建过程的一部分......但是我不明白为什么我需要
    • 我在尝试执行服务器端渲染时遇到了类似的错误。我收到一个未捕获的参考错误工作站未定义以下内容:
    • 我按照 reactjs.net 教程页面上的示例,将我的 jsx 文件放在包配置中,并将包引用包含在我的视图页面中。这对我不起作用。只有当我在我的视图页面中使用脚本标记专门引用 jsx 文件时,它才能正常工作
    • @Daniel Lo Nigro - 这应该在开发时重新编译吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2016-01-19
    • 1970-01-01
    相关资源
    最近更新 更多