In React, OK to always call ReactDOM.hydrate instead of ReactDOM.render?

后端 未结 2 1765
南笙
南笙 2021-02-14 19:26

I\'ve got code like the following where I\'m calling ReactDOM.hydrate. This is shared code that sometimes gets called from the node server and sometimes in the cli

2条回答
  •  执笔经年
    2021-02-14 19:53

    Strictly speaking, no it is not safe to always use ReactDOM.hydrate().

    From the docs on hydrate, you should only use it on "a container whose HTML contents were rendered by ReactDOMServer". hydrate also expects that the server rendered markup is identical to what the client side render outputs, and any differences should be considered bugs.

    ReactDOM.render() on the other hand is used to render your app into an empty container on the client. You may need to do this if you don't have server rendered markup on all pages.

    Because render() handles a use case that hydrate() does not, it is not safe to say "you can always use ReactDOM.hydrate()".

提交回复
热议问题