Reactjs - 创建工厂,将道具传递给组件

时间:2015-02-17 16:03:36

标签: reactjs

尝试实现以下目标:

if(typeof window === 'undefined'){
    return React.renderToString(React.createFactory(AppComponent)());
}
React.render(<AppComponent component={component} />, document.body);

component方法通过createFactory方法时,我如何才能将renderApp: function(component){ if(typeof window === 'undefined'){ var appComponentFactory = React.createFactory(AppComponent); return React.renderToString(appComponentFactory({ component: component })); } React.render(<AppComponent component={component} />, document.body); } 道具传递给组件?

编辑:

搞定了:

{{1}}

1 个答案:

答案 0 :(得分:5)

如果您已经为React.render()电话转发JSX,那么您就不需要与工厂一起跳过篮球......

renderApp: function(component){
    var app = <AppComponent component={component}/>
    if (typeof window === 'undefined') {
      return React.renderToString(app);
    }
    React.render(app, document.body);
}

...因为它只是糖:

var app = React.createElement(AppComponent, {component: component})

这可能不适用,但如果您的AppComponent除了呈现component道具之外没有做任何事情,您也可以直接渲染该组件:

renderApp: function(Component) {
    var app = <Component/>
    if (typeof window === 'undefined') {
      return React.renderToString(app);
    }
    React.render(app, document.body);
}

......再次展示了堕胎:

var app = React.createElement(Component)