使用ES6 ReactJs.Net服务器端rending时出错

时间:2017-03-27 17:55:41

标签: reactjs.net

我一直在使用ReactJs.Net和MVC5应用程序,并在使用ES6和babel转换器时面临服务器端rending的问题。我使用了expose-loader来全局公开组件。但它仅适用于ES5语法。如果我使用ES6和Webpack进行捆绑,我在@Html.React()函数中遇到以下错误。

渲染时出错" Hello" to" react_Hq6cd5QfaUu26XrhGgZA":脚本引发异常:Minified React错误#130;访问http://facebook.github.io/react/docs/error-decoder.html

〜Jsx / Components / Helloworld.jsx的代码



import React, {Component} from 'react';

export default class Helloworld extends Component {
    render() {
        return (
          <div>Hello {this.props.name} ,This is my first react component.</div>
        );
          }
}

Code for ~Jsx/server.jsx

import Hello from 'expose?Hello!./components/Helloworld' ;


webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = 
{
    entry: ['./Jsx/server.jsx'],
    output: { path: './build', filename: 'main.js' },
    module: {
        loaders: [
          {
              test: /\.jsx$/,
              loader: ['babel-loader'],
              include: path.resolve(__dirname, 'Jsx'),
              query: {
                  presets: ['es2015', 'react']
              }
          },
        ]
       
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    }
    
    //watch:true
}


RectConfig.cs

 ReactSiteConfiguration.Configuration
               .SetLoadBabel(false)
               .AddScriptWithoutTransform("~/build/main.js");

The above code works fine if i use the below  ES5 syntax for Helloworld component

var React=require('react');

var Helloworld = React.createClass({
    render: function() {
        return (
                  <div>
                      Hello {this.props.name} ,This is my first react component.
                  </div>
      );
}
});

module.exports=Helloworld;
&#13;
&#13;
&#13;

如果有人知道解决方案,请帮助我。

0 个答案:

没有答案