Skip to content

Render static html client-side #442

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
alfredoreduarte opened this issue Jan 16, 2016 · 3 comments
Closed

Render static html client-side #442

alfredoreduarte opened this issue Jan 16, 2016 · 3 comments

Comments

@alfredoreduarte
Copy link

Is there any way to get a string of plain html from a react component, but on the client side?

My app has a design editor that lets you click elements and edit their style, nothing new there, but I'm re-making it from the ground up using react-rails both for the app itself and for the templates that should be edited inside an iframe and then rendered elsewhere.

Attaching click handlers to every editable element conditionally (eg: if the view is being rendered inside an iframe, for the editor) would be a nightmare, and I can't just use jquery $('.is-editable').click() like I used to because of the virtual DOM.

Taking all that in account, it seems that the only solution is rendering the would-be-edited view as plain HTML so I can use the $('.is-editable').click() approach inside my editor.

Any insights or similar experiences?

@rmosolgo
Copy link
Member

Is there any way to get a string of plain html from a react component, but on the client side?

You can use ReactDOMServer.renderToString or ReactDOMServer.renderToStaticMarkup (https://facebook.github.io/react/docs/top-level-api.html#reactdomserver)

To get the ReactDOMServer object, switch this line:

// remove this:
//= require react 

for this line:

// add this: 
//= require react-server

(this gives you the server bundle).

I use this in my own app for integrating with other JS libraries that expect strings of HTML!

Does that answer your question?

@rmosolgo
Copy link
Member

(I'd like to improve that //= require API so you could require the objects one-by-one instead of picking an off-the-shelf bundle)

@rmosolgo rmosolgo mentioned this issue Jan 22, 2016
17 tasks
@rmosolgo rmosolgo closed this as completed Dec 9, 2016
@michelson
Copy link

Hi , i'm trying to include the server bundle but the ReactDOM reference is lost, so ReactUJS fails

image

any hints? cheers

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

No branches or pull requests

3 participants