Skip to content

Allow control of the container ID #50

@jhemminger

Description

@jhemminger

Hi - I need to be able to control the value of the ID that is currently being generated as "react#" - I thought you may have fixed this as part of #45 but instead that change allows the user to define the element type. Any chance you could include the ability to change the id? I think others would be happy to have this ability as well.

Your code is setup well to allow for this change easily, something like so is all I need:

Addition on the Extension:

public static IHtmlString React(
this HtmlHelper htmlHelper,
string componentName,
T props,
string htmlTag = null,
string htmlId = null
)
{
var reactComponent = Environment.CreateComponent(componentName, htmlId, props);
if (!string.IsNullOrEmpty(htmlTag))
{
reactComponent.ContainerTag = htmlTag;
}
var result = reactComponent.RenderHtml();
return new HtmlString(result);
}

Addition on ReactEnvironment:

public virtual IReactComponent CreateComponent(string componentName, string componentDomId, T props)
{
EnsureUserScriptsLoaded();

if (string.IsNullOrEmpty(componentDomId))
{
_maxContainerId++;
componentDomId = string.Format(CONTAINER_ELEMENT_NAME, _maxContainerId);
}

var component = new ReactComponent(this, _config, componentName, componentDomId)
{
Props = props
};
_components.Add(component);
return component;
}

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions