I need to render top-level html tags on the client-side (for example, <html><head>...</head><body></body></html>
). The result will be injected into an iframe. On the server, I would use the renderToStaticMarkup
function from react-dom/server
, but the client-only react-dom
doesn't have this function.
Will react-dom/server
work on the client in the latest version of react (currently 15.3.0)? If so, is there any indication from the react dev team that it will continue to be supported on the client in future versions?
tl;dr: yes.
I verified that it works in react@15.3.0
. The facebook/react team actually left it as part of react itself but only exposed it as React.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
. If you want, you could use it like that directly:
var ReactDOMServer = React.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
console.log(ReactDOMServer.renderToStaticMarkup(<div />)); // => <div></div>
Of course, you'll probably get fired if you do that...
Luckily, they also created a client-side version of react-dom/server (react-dom-server-15.3.0.min.js
). There's not much to that script. All it does is expose React.__SECRET_DOM_SERVER_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
as ReactDOMServer (using a UMD wrapper).
I can only assume that if the react team went through the effort to create a browser build for it, it is intended to continue working on the client-side and will continue to be supported in the future.
来源:https://stackoverflow.com/questions/38666241/does-react-dom-server-work-on-the-client-side