Skip to content

Commit 4c06bd8

Browse files
committed
Add option not to render the React data-attributes during server side rendering
1 parent e56c9be commit 4c06bd8

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,51 @@ private static IReactEnvironment Environment
6363
/// <typeparam name="T">Type of the props</typeparam>
6464
/// <param name="htmlHelper">HTML helper</param>
6565
/// <param name="componentName">Name of the component</param>
66-
/// <param name="props">Props to initialise the component with</param>
66+
/// <param name="props">Props to initialize the component with</param>
6767
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
6868
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
69-
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
69+
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialization code. Defaults to <c>false</c></param>
70+
/// <param name="renderReactAttributes">Indicates if the React data-attributes should be rendered during server side rendering</param>
7071
/// <returns>The component's HTML</returns>
7172
public static IHtmlString React<T>(
7273
this IHtmlHelper htmlHelper,
7374
string componentName,
7475
T props,
7576
string htmlTag = null,
7677
string containerId = null,
77-
bool clientOnly = false
78+
bool clientOnly = false,
79+
bool renderReactAttributes = true
7880
)
7981
{
8082
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
8183
if (!string.IsNullOrEmpty(htmlTag))
8284
{
8385
reactComponent.ContainerTag = htmlTag;
8486
}
85-
var result = reactComponent.RenderHtml(clientOnly);
87+
var result = reactComponent.RenderHtml(clientOnly, renderReactAttributes);
8688
return new HtmlString(result);
8789
}
8890

8991
/// <summary>
90-
/// Renders the specified React component, along with its client-side initialisation code.
92+
/// Renders the specified React component, along with its client-side initialization code.
9193
/// Normally you would use the <see cref="React{T}"/> method, but <see cref="ReactWithInit{T}"/>
9294
/// is useful when rendering self-contained partial views.
9395
/// </summary>
9496
/// <typeparam name="T">Type of the props</typeparam>
9597
/// <param name="htmlHelper">HTML helper</param>
9698
/// <param name="componentName">Name of the component</param>
97-
/// <param name="props">Props to initialise the component with</param>
99+
/// <param name="props">Props to initialize the component with</param>
98100
/// <param name="htmlTag">HTML tag to wrap the component in. Defaults to &lt;div&gt;</param>
99101
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
100-
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialisation code. Defaults to <c>false</c></param>
102+
/// <param name="clientOnly">Skip rendering server-side and only output client-side initialization code. Defaults to <c>false</c></param>
101103
/// <returns>The component's HTML</returns>
102104
public static IHtmlString ReactWithInit<T>(
103105
this IHtmlHelper htmlHelper,
104106
string componentName,
105107
T props,
106108
string htmlTag = null,
107-
string containerId = null,
108-
bool clientOnly = false
109+
string containerId = null,
110+
bool clientOnly = false
109111
)
110112
{
111113
var reactComponent = Environment.CreateComponent(componentName, props, containerId);

src/React.Core/IReactComponent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public interface IReactComponent
3939
/// return the rendered HTML.
4040
/// </summary>
4141
/// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
42+
/// <param name="renderReactAttributes">Indicates if the React data-attributes should be rendered during server side rendering</param>
4243
/// <returns>HTML</returns>
43-
string RenderHtml(bool renderContainerOnly = false);
44+
string RenderHtml(bool renderContainerOnly = false, bool renderReactAttributes = true);
4445

4546
/// <summary>
4647
/// Renders the JavaScript required to initialise this component client-side. This will

src/React.Core/ReactComponent.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,20 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
7979
/// return the rendered HTML.
8080
/// </summary>
8181
/// <param name="renderContainerOnly">Only renders component container. Used for client-side only rendering.</param>
82+
/// <param name="renderReactAttributes">Indicates if the React data-attributes should be rendered during server side rendering</param>
8283
/// <returns>HTML</returns>
83-
public virtual string RenderHtml(bool renderContainerOnly = false)
84+
public virtual string RenderHtml(bool renderContainerOnly = false, bool renderReactAttributes = true)
8485
{
8586
EnsureComponentExists();
8687
try
8788
{
8889
var html = string.Empty;
8990
if (!renderContainerOnly)
90-
{
91-
html = _environment.Execute<string>(
92-
string.Format("React.renderToString({0})", GetComponentInitialiser())
93-
);
91+
{
92+
var reactRenderCommand = renderReactAttributes
93+
? string.Format("React.renderToString({0})", GetComponentInitialiser())
94+
: string.Format("React.renderToStaticMarkup({0})", GetComponentInitialiser());
95+
html = _environment.Execute<string>(reactRenderCommand);
9496
}
9597
return string.Format(
9698
"<{2} id=\"{0}\">{1}</{2}>",

0 commit comments

Comments
 (0)