Skip to content
Prev Previous commit
Next Next commit
Only render JavaScript if component is not server-side only
  • Loading branch information
Gustav Tonér committed Jan 15, 2018
commit f0ceb0cc75ba3adc77f08dbb736a21a8019af9cf
4 changes: 2 additions & 2 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 @@ -69,7 +69,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: 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
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