Skip to content

Commit 32d8470

Browse files
committed
Pass IReactSiteConfiguration as parameter for ReactComponent ctor
1 parent c2f7cf6 commit 32d8470

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

src/React.Tests/Core/ReactComponentTest.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void RenderHtmlShouldThrowExceptionIfComponentDoesNotExist()
2121
{
2222
var environment = new Mock<IReactEnvironment>();
2323
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(false);
24-
var component = new ReactComponent(environment.Object, "Foo", "container");
24+
var component = new ReactComponent(environment.Object, null, "Foo", "container");
2525

2626
Assert.Throws<ReactInvalidComponentException>(() =>
2727
{
@@ -34,8 +34,9 @@ public void RenderHtmlShouldCallRenderComponent()
3434
{
3535
var environment = new Mock<IReactEnvironment>();
3636
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
37+
var config = new Mock<IReactSiteConfiguration>();
3738

38-
var component = new ReactComponent(environment.Object, "Foo", "container")
39+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
3940
{
4041
Props = new { hello = "World" }
4142
};
@@ -51,8 +52,9 @@ public void RenderHtmlShouldWrapComponentInDiv()
5152
environment.Setup(x => x.Execute<bool>("typeof Foo !== 'undefined'")).Returns(true);
5253
environment.Setup(x => x.Execute<string>(@"React.renderToString(Foo({""hello"":""World""}))"))
5354
.Returns("[HTML]");
55+
var config = new Mock<IReactSiteConfiguration>();
5456

55-
var component = new ReactComponent(environment.Object, "Foo", "container")
57+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
5658
{
5759
Props = new { hello = "World" }
5860
};
@@ -65,8 +67,9 @@ public void RenderHtmlShouldWrapComponentInDiv()
6567
public void RenderJavaScriptShouldCallRenderComponent()
6668
{
6769
var environment = new Mock<IReactEnvironment>();
70+
var config = new Mock<IReactSiteConfiguration>();
6871

69-
var component = new ReactComponent(environment.Object, "Foo", "container")
72+
var component = new ReactComponent(environment.Object, config.Object, "Foo", "container")
7073
{
7174
Props = new { hello = "World" }
7275
};

src/React/IReactEnvironment.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,5 @@ public interface IReactEnvironment
107107
/// Gets the JSX Transformer for this environment.
108108
/// </summary>
109109
IJsxTransformer JsxTransformer { get; }
110-
111-
/// <summary>
112-
/// Gets the global site configuration.
113-
/// </summary>
114-
IReactSiteConfiguration Configuration { get; }
115110
}
116111
}

src/React/IReactSiteConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public interface IReactSiteConfiguration
4545
IReactSiteConfiguration SetUseHarmony(bool useHarmony);
4646

4747
/// <summary>
48-
/// Gets the configuration for json serializer.
48+
/// Gets or sets the configuration for JSON serializer.
4949
/// </summary>
50-
JsonSerializerSettings JsonSerializerSettings { get; }
50+
JsonSerializerSettings JsonSerializerSettings { get; set; }
5151

5252
/// <summary>
5353
/// Sets the configuration for json serializer.

src/React/ReactComponent.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public class ReactComponent : IReactComponent
3232
/// </summary>
3333
private readonly IReactEnvironment _environment;
3434

35+
/// <summary>
36+
/// Global site configuration
37+
/// </summary>
38+
private readonly IReactSiteConfiguration _configuration;
39+
3540
/// <summary>
3641
/// Name of the component
3742
/// </summary>
@@ -51,12 +56,14 @@ public class ReactComponent : IReactComponent
5156
/// Initializes a new instance of the <see cref="ReactComponent"/> class.
5257
/// </summary>
5358
/// <param name="environment">The environment.</param>
59+
/// <param name="configuration">Site-wide configuration.</param>
5460
/// <param name="componentName">Name of the component.</param>
5561
/// <param name="containerId">The ID of the container DIV for this component</param>
56-
public ReactComponent(IReactEnvironment environment, string componentName, string containerId)
62+
public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration configuration, string componentName, string containerId)
5763
{
5864
EnsureComponentNameValid(componentName);
5965
_environment = environment;
66+
_configuration = configuration;
6067
_componentName = componentName;
6168
_containerId = containerId;
6269
}
@@ -103,7 +110,7 @@ public string RenderJavaScript()
103110
return string.Format(
104111
"React.render({0}, document.getElementById({1}))",
105112
GetComponentInitialiser(),
106-
JsonConvert.SerializeObject(_containerId, _environment.Configuration.JsonSerializerSettings)
113+
JsonConvert.SerializeObject(_containerId, _configuration.JsonSerializerSettings) // SerializeObject accepts null settings
107114
);
108115
}
109116

@@ -133,7 +140,7 @@ private void EnsureComponentExists()
133140
/// <returns>JavaScript for component initialisation</returns>
134141
private string GetComponentInitialiser()
135142
{
136-
var encodedProps = JsonConvert.SerializeObject(Props, _environment.Configuration.JsonSerializerSettings);
143+
var encodedProps = JsonConvert.SerializeObject(Props, _configuration.JsonSerializerSettings); // SerializeObject accepts null settings
137144
return string.Format(
138145
"{0}({1})",
139146
_componentName,

src/React/ReactEnvironment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public IReactComponent CreateComponent<T>(string componentName, T props)
268268
EnsureUserScriptsLoaded();
269269
_maxContainerId++;
270270
var containerId = string.Format(CONTAINER_ELEMENT_NAME, _maxContainerId);
271-
var component = new ReactComponent(this, componentName, containerId)
271+
var component = new ReactComponent(this, _config, componentName, containerId)
272272
{
273273
Props = props
274274
};

src/React/ReactSiteConfiguration.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ static ReactSiteConfiguration()
3333
/// </summary>
3434
private readonly IList<string> _scriptFiles = new List<string>();
3535

36-
private JsonSerializerSettings _jsonSerializerSettings;
37-
3836
/// <summary>
3937
/// Adds a script to the list of scripts that are executed. This should be called for all
4038
/// React components and their dependencies.
@@ -74,11 +72,12 @@ public IReactSiteConfiguration SetUseHarmony(bool useHarmony)
7472
}
7573

7674
/// <summary>
77-
/// Gets the configuration for json serializer.
75+
/// Gets or sets the configuration for JSON serializer.
7876
/// </summary>
7977
public JsonSerializerSettings JsonSerializerSettings
8078
{
81-
get { return _jsonSerializerSettings; }
79+
get;
80+
set;
8281
}
8382

8483
/// <summary>
@@ -91,7 +90,7 @@ public JsonSerializerSettings JsonSerializerSettings
9190
/// </remarks>
9291
public IReactSiteConfiguration SetJsonSerializerSettings(JsonSerializerSettings settings)
9392
{
94-
_jsonSerializerSettings = settings;
93+
JsonSerializerSettings = settings;
9594
return this;
9695
}
9796
}

0 commit comments

Comments
 (0)