简体   繁体   English

react es6 服务器端渲染不需要工厂?

[英]react es6 server side rendering doesn't require a factory?

I was writing an app with react and to perform server-side rendering I did the following:我正在编写一个带有反应的应用程序并执行服务器端渲染,我执行了以下操作:

var Component = require('myComponent');
var ComponentFactory = React.createFactory(Component);

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', {
        react: ReactDom.renderToString(ComponentFactory()),
    });
});

Now I'm rewriting the app but switched most of the javascript to ES6, but when I tried to create a factory with the component, it would not show up in the browser, instead I had to do the following:现在我正在重写应用程序,但将大部分 javascript 切换到 ES6,但是当我尝试使用组件创建工厂时,它不会显示在浏览器中,而是我必须执行以下操作:

var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', { 
        reacthtml: ReactDom.renderToString(<InfoBoxes />)***here***
    });
});

I had to just input the component in jsx syntax.我只需要以 jsx 语法输入组件。 Is this ES6 dependent or is there another reason why this may happen?这是依赖 ES6 还是有其他原因导致这种情况发生?

JSX like this: JSX 像这样:

<InfoBoxes />

Is actually transpiled into this:实际上转译成这样:

React.createElement(InfoBoxes)

And createFactory looks (roughly) like this: createFactory看起来(大致)是这样的:

function createFactory(type) {
    return React.createElement.bind(null, type);
}

So they are ultimately equivalent;所以它们最终是等价的; you don't need to use factories if you're using jsx.如果您使用 jsx,则不需要使用工厂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM