Does react-dom/server work on the client-side?

佐手、 提交于 2019-12-05 22:25:55

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!