Skip to content

Commit 341f27b

Browse files
committed
Merge branch 'master' of https://github.com/reactjs/React.NET
2 parents 8e88f42 + 7d80f64 commit 341f27b

File tree

9 files changed

+131
-13
lines changed

9 files changed

+131
-13
lines changed

site/jekyll/_posts/2015-03-02-1.4.0-release.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ I'm happy to announce the release of ReactJS.NET 1.4! This release adds support
88

99
Full list of changes:
1010

11-
* [#47](https://github.com/reactjs/React.NET/issues/47) and [#94](https://github.com/reactjs/React.NET/pull/94) — Support for ASP.NET 5. You must be using Visual Studio 2015 CTP6 and ASP.NET 5 Beta 3. Documentation will be added to the site shortly.
11+
* [#47](https://github.com/reactjs/React.NET/issues/47) and [#94](https://github.com/reactjs/React.NET/pull/94)[Support for ASP.NET 5](/getting-started/aspnet5.html). You must be using Visual Studio 2015 CTP6 and ASP.NET 5 Beta 3. Documentation will be added to the site shortly.
1212
* [#86](https://github.com/reactjs/React.NET/issues/86)`console` calls such as `console.log` during server-side rendering will automatically be propagated to the client-side. This can greatly assist in debugging server-side rendering. Nicer debugging tools will come in the future!
1313
* [#96](https://github.com/reactjs/React.NET/issues/96) — Bundle V8 support by default, stop relying on MSIE engine as much.
1414
* [#97](https://github.com/reactjs/React.NET/issues/97) — Upgrade to JSPool 0.2. This improves the handling of JavaScript engines by recycling them after a number of uses, which ensures memory usage doesn't keep growing over time.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
id: aspnet5
3+
layout: docs
4+
title: Getting Started on ASP.NET 5
5+
---
6+
7+
Getting started with ReactJS.NET on ASP.NET 5 and MVC 6 requires a few more steps compared to previous versions of ASP.NET and MVC. A more fully featured tutorial will be released once the stable release of ASP.NET 5 is out.
8+
9+
Note that ASP.NET 5 is still in beta, and so there may still be some sharp edges. ReactJS.NET requires at least Visual Studio 2015 CTP6 and ASP.NET 5 Beta 3. Additionally, ReactJS.NET does not support the Core CLR at this point in time, so you will need to ensure your project is not referencing it. Remove the `"aspnetcore50": { }` line from your `project.json` file.
10+
11+
Once this has been removed, install the `React.AspNet` package through NuGet. After the package is installed, ReactJS.NET needs to be initialised in your `Startup.cs` file (unfortunately this can not be done automatically like in previous versions of ASP.NET with WebActivator). At the top of the file, add:
12+
```
13+
using React.AspNet;
14+
```
15+
16+
Directly above:
17+
18+
```csharp
19+
// Add MVC services to the services container.
20+
services.AddMvc();
21+
```
22+
23+
Add:
24+
25+
```csharp
26+
services.AddReact();
27+
```
28+
29+
30+
Directly above:
31+
32+
```csharp
33+
// Add static files to the request pipeline.
34+
app.UseStaticFiles();
35+
```
36+
37+
Add:
38+
39+
```csharp
40+
app.UseRequestServices();
41+
app.UseReact(config =>
42+
{
43+
// ES6 features are enabled by default. Uncomment the below line to disable them.
44+
// See http://reactjs.net/guides/es6.html for more information.
45+
//config.SetUseHarmony(false);
46+
// Uncomment the below line if you are using Flow
47+
// See http://reactjs.net/guides/flow.html for more information.
48+
//config.SetStripTypes(true);
49+
// If you want to use server-side rendering of React components,
50+
// add all the necessary JavaScript files here. This includes
51+
// your components as well as all of their dependencies.
52+
// See http://reactjs.net/ for more information. Example:
53+
//config
54+
// .AddScript("~/Scripts/First.jsx")
55+
// .AddScript("~/Scripts/Second.jsx");
56+
});
57+
```
58+
59+
Finally, add this to `Views/_GlobalImport.cshtml` (or create it if it doesn't exist):
60+
61+
```csharp
62+
@using React.AspNet
63+
```
64+
65+
Once ReactJS.NET has been configured, you will be able to use [on-the-fly JSX to JavaScript compilation](http://reactjs.net/getting-started/usage.html) and [server-side rendering](http://reactjs.net/guides/server-side-rendering.html).

site/jekyll/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ id: home
2020
other .NET languages, focusing specifically on ASP.NET MVC (although it
2121
also works in other environments). It assumes you already have some basic
2222
knowledge about React. It is cross-platform and can run on Linux via Mono.
23+
Now with support for [ASP.NET 5 Beta](/getting-started/aspnet5.html)!
2324
Take a look at [the tutorial](/getting-started/tutorial.html) to see how
2425
easy it is to get started with React and ReactJS.NET!
2526
</p>

site/public/2015

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../jekyll/_site/2015

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,23 @@ private static IReactEnvironment Environment
6666
/// <param name="props">Props to initialise the component with</param>
6767
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
6868
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
69+
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
6970
/// <returns>The component's HTML</returns>
7071
public static IHtmlString React<T>(
7172
this IHtmlHelper htmlHelper,
7273
string componentName,
7374
T props,
7475
string htmlTag = null,
75-
string containerId = null
76+
string containerId = null,
77+
bool clientOnly = false
7678
)
7779
{
7880
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
7981
if (!string.IsNullOrEmpty(htmlTag))
8082
{
8183
reactComponent.ContainerTag = htmlTag;
8284
}
83-
var result = reactComponent.RenderHtml();
85+
var result = reactComponent.RenderHtml(clientOnly);
8486
return new HtmlString(result);
8587
}
8688

@@ -95,21 +97,23 @@ public static IHtmlString React<T>(
9597
/// <param name="props">Props to initialise the component with</param>
9698
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
9799
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
100+
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
98101
/// <returns>The component's HTML</returns>
99102
public static IHtmlString ReactWithInit<T>(
100103
this IHtmlHelper htmlHelper,
101104
string componentName,
102105
T props,
103106
string htmlTag = null,
104-
string containerId = null
107+
string containerId = null,
108+
bool clientOnly = false
105109
)
106110
{
107111
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
108112
if (!string.IsNullOrEmpty(htmlTag))
109113
{
110114
reactComponent.ContainerTag = htmlTag;
111115
}
112-
var html = reactComponent.RenderHtml();
116+
var html = reactComponent.RenderHtml(clientOnly);
113117
var script = new TagBuilder("script")
114118
{
115119
InnerHtml = reactComponent.RenderJavaScript()
@@ -132,4 +136,4 @@ public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper)
132136
return new HtmlString(tag.ToString());
133137
}
134138
}
135-
}
139+
}

src/React.Core/IReactComponent.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public interface IReactComponent
3838
/// Renders the HTML for this component. This will execute the component server-side and
3939
/// return the rendered HTML.
4040
/// </summary>
41+
/// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
4142
/// <returns>HTML</returns>
42-
string RenderHtml();
43+
string RenderHtml(bool renderContainerOnly = false);
4344

4445
/// <summary>
4546
/// Renders the JavaScript required to initialise this component client-side. This will
@@ -49,4 +50,4 @@ public interface IReactComponent
4950
/// <returns>JavaScript</returns>
5051
string RenderJavaScript();
5152
}
52-
}
53+
}

src/React.Core/ReactComponent.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,20 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
7878
/// Renders the HTML for this component. This will execute the component server-side and
7979
/// return the rendered HTML.
8080
/// </summary>
81+
/// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
8182
/// <returns>HTML</returns>
82-
public virtual string RenderHtml()
83+
public virtual string RenderHtml(bool renderContainerOnly = false)
8384
{
8485
EnsureComponentExists();
8586
try
86-
{
87-
var html = _environment.Execute<string>(
88-
string.Format("React.renderToString({0})", GetComponentInitialiser())
87+
{
88+
var html = string.Empty;
89+
if (!renderContainerOnly)
90+
{
91+
html = _environment.Execute<string>(
92+
string.Format("React.renderToString({0})", GetComponentInitialiser())
8993
);
94+
}
9095
return string.Format(
9196
"<{2} id=\"{0}\">{1}</{2}>",
9297
ContainerId,

src/React.Tests/Core/ReactComponentTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ public void RenderHtmlShouldWrapComponentInDiv()
6363
Assert.AreEqual(@"<div id=""container"">[HTML]</div>", result);
6464
}
6565

66+
[Test]
67+
public void RenderHtmlShouldNotRenderComponentHTML()
68+
{
69+
var environment = new Mock<IReactEnvironment>();
70+
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
71+
environment.Setup(x => x.Execute<string>(@"React.renderToString(React.createElement(Foo, {""hello"":""World""}))"))
72+
.Returns("[HTML]");
73+
var config = new Mock<IReactSiteConfiguration>();
74+
75+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
76+
{
77+
Props = new { hello = "World" }
78+
};
79+
var result = component.RenderHtml(renderContainerOnly: true);
80+
81+
Assert.AreEqual(@"<div id=""container""></div>", result);
82+
environment.Verify(x => x.Execute(It.IsAny<string>()), Times.Never);
83+
}
84+
6685
[Test]
6786
public void RenderHtmlShouldWrapComponentInCustomElement()
6887
{

src/React.Tests/Mvc/HtmlHelperExtensionsTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private Mock<IReactEnvironment> ConfigureMockEnvironment()
3333
public void ReactWithInitShouldReturnHtmlAndScript()
3434
{
3535
var component = new Mock<IReactComponent>();
36-
component.Setup(x => x.RenderHtml()).Returns("HTML");
36+
component.Setup(x => x.RenderHtml(false)).Returns("HTML");
3737
component.Setup(x => x.RenderJavaScript()).Returns("JS");
3838
var environment = ConfigureMockEnvironment();
3939
environment.Setup(x => x.CreateComponent(
@@ -54,5 +54,27 @@ public void ReactWithInitShouldReturnHtmlAndScript()
5454
);
5555

5656
}
57+
58+
[Test]
59+
public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
60+
{
61+
var component = new Mock<IReactComponent>();
62+
component.Setup(x => x.RenderHtml(true)).Returns("HTML");
63+
var environment = ConfigureMockEnvironment();
64+
environment.Setup(x => x.CreateComponent(
65+
"ComponentName",
66+
new {},
67+
null
68+
)).Returns(component.Object);
69+
70+
var result = HtmlHelperExtensions.React(
71+
htmlHelper: null,
72+
componentName: "ComponentName",
73+
props: new { },
74+
htmlTag: "span",
75+
clientOnly: true
76+
);
77+
component.Verify(x => x.RenderHtml(It.Is<bool>(y => y == true)), Times.Once);
78+
}
5779
}
5880
}

0 commit comments

Comments
 (0)