Simplify ReactRenderer API when referencing default modules

This commit is contained in:
SteveSandersonMS
2015-11-02 20:23:05 -08:00
parent 7e1955c6fe
commit 0c59f670b2
5 changed files with 7 additions and 4 deletions

View File

@@ -27,7 +27,7 @@ module.exports = {
renderToString: function(callback, options) { renderToString: function(callback, options) {
var resolvedPath = path.resolve(process.cwd(), options.moduleName); var resolvedPath = path.resolve(process.cwd(), options.moduleName);
var requestedModule = require(resolvedPath); var requestedModule = require(resolvedPath);
var component = requestedModule[options.exportName]; var component = options.exportName ? requestedModule[options.exportName] : requestedModule;
if (!component) { if (!component) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + options.exportName + '"'); throw new Error('The module "' + resolvedPath + '" has no export named "' + options.exportName + '"');
} }

View File

@@ -12,6 +12,10 @@ namespace Microsoft.AspNet.NodeServices.React
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
} }
public static Task<string> RenderToString(INodeServices nodeServices, string moduleName, string baseUrl) {
return RenderToString(nodeServices, moduleName, /* exportName */ null, baseUrl);
}
public static async Task<string> RenderToString(INodeServices nodeServices, string moduleName, string exportName, string baseUrl) { public static async Task<string> RenderToString(INodeServices nodeServices, string moduleName, string exportName, string baseUrl) {
return await nodeServices.InvokeExport(nodeScript.FileName, "renderToString", new { return await nodeServices.InvokeExport(nodeScript.FileName, "renderToString", new {
moduleName, moduleName,

View File

@@ -17,7 +17,6 @@ namespace ReactExample.Controllers
{ {
ViewData["ReactOutput"] = await ReactRenderer.RenderToString(this.nodeServices, ViewData["ReactOutput"] = await ReactRenderer.RenderToString(this.nodeServices,
moduleName: "ReactApp/components/ReactApp.jsx", moduleName: "ReactApp/components/ReactApp.jsx",
exportName: "ReactApp",
baseUrl: Request.Path baseUrl: Request.Path
); );
return View(); return View();

View File

@@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import createBrowserHistory from 'history/lib/createBrowserHistory'; import createBrowserHistory from 'history/lib/createBrowserHistory';
import { ReactApp } from './components/ReactApp.jsx'; import ReactApp from './components/ReactApp.jsx';
// In the browser, we render into a DOM node and hook up to the browser's history APIs // In the browser, we render into a DOM node and hook up to the browser's history APIs
var history = createBrowserHistory(); var history = createBrowserHistory();

View File

@@ -2,7 +2,7 @@ import React from 'react';
import { Router, Route } from 'react-router'; import { Router, Route } from 'react-router';
import { PeopleGrid } from './PeopleGrid.jsx'; import { PeopleGrid } from './PeopleGrid.jsx';
export class ReactApp extends React.Component { export default class ReactApp extends React.Component {
render() { render() {
return ( return (
<Router history={this.props.history}> <Router history={this.props.history}>