Skip to content

Add additional option to include initialization script for Html.React #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

BartAdv
Copy link
Contributor

@BartAdv BartAdv commented Nov 4, 2014

In my scenario I was using react component in a page that was rendered using partial views fetched via AJAX, so one call for @Html.ReactInitJavaScript() at the beginning was not enough. I wanted to run initialization script just after rendering server-side content and this additional flag seemed to fulfill my requirements.

@Daniel15
Copy link
Member

Daniel15 commented Nov 6, 2014

I don't imagine this being a very common use case so I'd prefer having this as a separate helper method (Html.ReactWithInitialize or something like that?) to keep the core Html.React helper clean and free from arbitrary parameters.

@Daniel15
Copy link
Member

Daniel15 commented Nov 6, 2014

Also what happens if you render it with the init code here, and then also have a Html.ReactInitJavaScript call on the page? I think the same init code will be rendered twice. You may need to add an _initRendered flag on the component that's set when the init code is rendered so it's only ever rendered twice.

@BartAdv
Copy link
Contributor Author

BartAdv commented Nov 6, 2014

Agreed it's highly uncommon scenario, will refactor it.

However I've got some issues with the flag. As I see it, it would have to be a property (but we can't make it with internal setter), because it shouldn't be set by component itself when we call RenderJavaScript (as it would make it side-effecting). And property with public setter would be useless. Maybe Environment should maintain the list of components and keep this additional information there? Or maybe there is no need for such check, after all, if someone is explicitly rendering initialization code after rendering component, he's aware why he's doing so.

@@ -28,6 +28,13 @@ private static IReactEnvironment Environment
get { return AssemblyRegistration.Container.Resolve<IReactEnvironment>(); }
}

private static IReactComponent CreateComponentAndRender<T>(string componentName, T props, out string html)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if having this as a separate method adds value, it may be better to just inline the Environment.CreateComponent call in both of the methods below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And then it would check if component haven't been rendered already?

@Daniel15 Daniel15 closed this in d1d8266 Nov 23, 2014
@Daniel15
Copy link
Member

Thanks for your contribution! I implemented a very slightly modified version (it was mostly the same, just some small changes) along with a unit test. I'll credit you in the release notes :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants