Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/React.Core/IReactEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public interface IReactEnvironment
/// <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>
/// <param name="skipLazyInit">Skip adding to components list, which is used during GetInitJavascript</param>
/// <returns>The component</returns>
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false);
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false, bool skipLazyInit = false);

/// <summary>
/// Adds the provided <see cref="IReactComponent"/> to the list of components to render client side.
Expand Down
9 changes: 7 additions & 2 deletions src/React.Core/ReactEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,9 @@ public virtual bool HasVariable(string name)
/// <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>
/// <param name="skipLazyInit">Skip adding to components list, which is used during GetInitJavascript</param>
/// <returns>The component</returns>
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false)
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false, bool skipLazyInit = false)
{
if (!clientOnly)
{
Expand All @@ -287,7 +288,11 @@ public virtual IReactComponent CreateComponent<T>(string componentName, T props,
Props = props,
ServerOnly = serverOnly
};
_components.Add(component);

if (!skipLazyInit)
{
_components.Add(component);
}
return component;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/React.Tests/Core/ReactEnvironmentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ public void StyleTagsReturned()
Assert.Equal("static/css/another-stylesheet.css", styles[1]);
}

[Fact]
public void SkipLazyInit()
{
var mocks = new Mocks();
var environment = mocks.CreateReactEnvironment();

environment.CreateComponent("ComponentName", new { }, skipLazyInit: true);
Assert.Equal("", environment.GetInitJavaScript());
}

public class Mocks
{
public Mock<PooledJsEngine> Engine { get; private set; }
Expand Down
10 changes: 8 additions & 2 deletions tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void ReactWithInitShouldReturnHtmlAndScript()
new { },
null,
false,
false,
false
)).Returns(component.Object);

Expand Down Expand Up @@ -109,6 +110,7 @@ public void ScriptNonceIsReturned()
new { },
null,
false,
false,
false
)).Returns(component.Object);

Expand Down Expand Up @@ -154,6 +156,7 @@ public void EngineIsReturnedToPoolAfterRender()
new { },
null,
true,
false,
false
)).Returns(component.Object);

Expand Down Expand Up @@ -184,6 +187,7 @@ public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
new { },
null,
true,
false,
false
)).Returns(component.Object);

Expand Down Expand Up @@ -212,7 +216,8 @@ public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue()
new { },
null,
false,
true
true,
false
)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
Expand Down Expand Up @@ -250,7 +255,8 @@ public void RenderFunctionsCalledNonLazily()
new { },
null,
false,
true
true,
false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use named arguments for all these booleans to make it clearer (eg. skipLazyInit: false)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regrettably, msbuild does not allow that in this case.

An expression tree may not contain a named argument specification

)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
Expand Down