Skip to content

Commit 99a94a1

Browse files
Skip adding to _components during ReactWithInit (#1155)
1 parent 0908329 commit 99a94a1

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/React.Core/IReactEnvironment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ public interface IReactEnvironment
8282
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
8383
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
8484
/// <param name="serverOnly">True if this component only should be rendered server-side. Defaults to false.</param>
85+
/// <param name="skipLazyInit">Skip adding to components list, which is used during GetInitJavascript</param>
8586
/// <returns>The component</returns>
86-
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false);
87+
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false, bool skipLazyInit = false);
8788

8889
/// <summary>
8990
/// Adds the provided <see cref="IReactComponent"/> to the list of components to render client side.

src/React.Core/ReactEnvironment.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,9 @@ public virtual bool HasVariable(string name)
273273
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
274274
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
275275
/// <param name="serverOnly">True if this component only should be rendered server-side. Defaults to false.</param>
276+
/// <param name="skipLazyInit">Skip adding to components list, which is used during GetInitJavascript</param>
276277
/// <returns>The component</returns>
277-
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false)
278+
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false, bool serverOnly = false, bool skipLazyInit = false)
278279
{
279280
if (!clientOnly)
280281
{
@@ -287,7 +288,11 @@ public virtual IReactComponent CreateComponent<T>(string componentName, T props,
287288
Props = props,
288289
ServerOnly = serverOnly
289290
};
290-
_components.Add(component);
291+
292+
if (!skipLazyInit)
293+
{
294+
_components.Add(component);
295+
}
291296
return component;
292297
}
293298

tests/React.Tests/Core/ReactEnvironmentTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ public void StyleTagsReturned()
218218
Assert.Equal("static/css/another-stylesheet.css", styles[1]);
219219
}
220220

221+
[Fact]
222+
public void SkipLazyInit()
223+
{
224+
var mocks = new Mocks();
225+
var environment = mocks.CreateReactEnvironment();
226+
227+
environment.CreateComponent("ComponentName", new { }, skipLazyInit: true);
228+
Assert.Equal("", environment.GetInitJavaScript());
229+
}
230+
221231
public class Mocks
222232
{
223233
public Mock<PooledJsEngine> Engine { get; private set; }

tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void ReactWithInitShouldReturnHtmlAndScript()
4949
new { },
5050
null,
5151
false,
52+
false,
5253
false
5354
)).Returns(component.Object);
5455

@@ -109,6 +110,7 @@ public void ScriptNonceIsReturned()
109110
new { },
110111
null,
111112
false,
113+
false,
112114
false
113115
)).Returns(component.Object);
114116

@@ -154,6 +156,7 @@ public void EngineIsReturnedToPoolAfterRender()
154156
new { },
155157
null,
156158
true,
159+
false,
157160
false
158161
)).Returns(component.Object);
159162

@@ -184,6 +187,7 @@ public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
184187
new { },
185188
null,
186189
true,
190+
false,
187191
false
188192
)).Returns(component.Object);
189193

@@ -212,7 +216,8 @@ public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue()
212216
new { },
213217
null,
214218
false,
215-
true
219+
true,
220+
false
216221
)).Returns(component.Object);
217222

218223
var result = HtmlHelperExtensions.React(
@@ -250,7 +255,8 @@ public void RenderFunctionsCalledNonLazily()
250255
new { },
251256
null,
252257
false,
253-
true
258+
true,
259+
false
254260
)).Returns(component.Object);
255261

256262
var result = HtmlHelperExtensions.React(

0 commit comments

Comments
 (0)