Skip to content
Closed
6 changes: 3 additions & 3 deletions src/React.AspNet/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2014-Present, Facebook, Inc.
* All rights reserved.
*
Expand Down Expand Up @@ -54,7 +54,7 @@ private static IReactEnvironment Environment
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
/// <param name="serverOnly">Skip rendering React specific data-attributes during server side rendering. Defaults to <c>false</c></param>
/// <param name="serverOnly">Skip rendering React specific data-attributes, container and client-side initialisation during server side rendering. Defaults to <c>false</c></param>
/// <param name="containerClass">HTML class(es) to set on the container tag</param>
/// <param name="exceptionHandler">A custom exception handler that will be called if a component throws during a render. Args: (Exception ex, string componentName, string containerId)</param>
/// <returns>The component's HTML</returns>
Expand All @@ -72,7 +72,7 @@ public static IHtmlString React<T>(
{
try
{
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly, serverOnly);
if (!string.IsNullOrEmpty(htmlTag))
{
reactComponent.ContainerTag = htmlTag;
Expand Down
5 changes: 5 additions & 0 deletions src/React.Core/IReactComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public interface IReactComponent
/// </summary>
string ContainerClass { get; set; }

/// <summary>
/// Get or sets if this components only should be rendered server side
/// </summary>
bool ServerOnly { get; set; }

/// <summary>
/// Renders the HTML for this component. This will execute the component server-side and
/// return the rendered HTML.
Expand Down
5 changes: 3 additions & 2 deletions src/React.Core/IReactEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2014-Present, Facebook, Inc.
* All rights reserved.
*
Expand Down Expand Up @@ -81,8 +81,9 @@ public interface IReactEnvironment
/// <param name="props">Props to use</param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
/// <param name="serverOnly">True if this component only should be rendered server-side. Defaults to false.</param>
/// <returns>The component</returns>
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false);
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false);

/// <summary>
/// Adds the provided <see cref="IReactComponent"/> to the list of components to render client side.
Expand Down
10 changes: 10 additions & 0 deletions src/React.Core/ReactComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public class ReactComponent : IReactComponent
/// </summary>
public string ContainerClass { get; set; }

/// <summary>
/// Get or sets if this components only should be rendered server side
/// </summary>
public bool ServerOnly { get; set; }

/// <summary>
/// Gets or sets the props for this component
/// </summary>
Expand Down Expand Up @@ -130,6 +135,11 @@ public virtual string RenderHtml(bool renderContainerOnly = false, bool renderSe
? string.Format("ReactDOMServer.renderToStaticMarkup({0})", GetComponentInitialiser())
: string.Format("ReactDOMServer.renderToString({0})", GetComponentInitialiser());
html = _environment.Execute<string>(reactRenderCommand);

if (renderServerOnly)
{
return html;
}
}
catch (JsRuntimeException ex)
{
Expand Down
15 changes: 10 additions & 5 deletions src/React.Core/ReactEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2014-Present, Facebook, Inc.
* All rights reserved.
*
Expand Down Expand Up @@ -287,8 +287,9 @@ public virtual bool HasVariable(string name)
/// <param name="props">Props to use</param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
/// <param name="serverOnly">True if this component only should be rendered server-side. Defaults to false.</param>
/// <returns>The component</returns>
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false)
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false)
{
if (!clientOnly)
{
Expand All @@ -297,7 +298,8 @@ public virtual IReactComponent CreateComponent<T>(string componentName, T props,

var component = new ReactComponent(this, _config, componentName, containerId)
{
Props = props
Props = props,
ServerOnly = serverOnly
};
_components.Add(component);
return component;
Expand Down Expand Up @@ -339,8 +341,11 @@ public virtual string GetInitJavaScript(bool clientOnly = false)

foreach (var component in _components)
{
fullScript.Append(component.RenderJavaScript());
fullScript.AppendLine(";");
if(!component.ServerOnly)
{
fullScript.Append(component.RenderJavaScript());
fullScript.AppendLine(";");
}
}

return fullScript.ToString();
Expand Down
Loading