What's the difference between hydrate() and render() in React 16?

后端 未结 6 833
我寻月下人不归
我寻月下人不归 2021-01-30 05:53

I\'ve read the documentation, but I didn\'t really understand the difference between hydrate() and render() in React 16.

I know hydrate()

相关标签:
6条回答
  • 2021-01-30 06:34

    From the ReactDOMServer docs (emphasis mine):

    If you call ReactDOM.hydrate() on a node that already has this server-rendered markup, React will preserve it and only attach event handlers, allowing you to have a very performant first-load experience.

    The text in bold is the main difference. render may change your node if there is a difference between the initial DOM and the current DOM. hydrate will only attach event handlers.

    From the Github issue that introduced hydrate as a separate API:

    If this is your initial DOM:

    <div id="container">
        <div class="spinner">Loading...</div>
    </div>
    

    and then call:

    ReactDOM.render(
       <div class="myapp">
          <span>App</span>
       </div>,
       document.getElementById('container')
    )
    

    intending to do a client-side only render (not hydration). Then you end with

    <div id="container">
       <div class="spinner">
           <span>App</span>
       </div>
    </div>
    

    Because we don't patch up the attributes.

    Just FYI the reason they didn't patch the attributes is

    ... This would be really slow to hydrate in the normal hydration mode and slow down initial render into a non-SSR tree.

    0 讨论(0)
  • 2021-01-30 06:38

    render will flush out anything in the specified element(named as 'root' in most cases) and rebuild it ,while hydrate will keep anything that is already inside the specified element and build from that,making the initial page load faster.

    0 讨论(0)
  • 2021-01-30 06:48

    Hydrate is basically used in case of SSR(Server side Rendering). SSR gives you the skeleton or HTML markup which is being shipped from a server so that for the first time when your page loads it is not blank and search engine bots can index it for SEO(A use case of SSR). So hydrate adds the JS to your page or a node to which SSR is applied. So that your page responds to the events performed by the user.

    Render is used for rendering the component on client side browser Plus if you try to replace the hydrate with render you will get a warning that render is deprecated and can't be used in case of SSR. it was removed because of it being slow as compared to hydrate.

    0 讨论(0)
  • 2021-01-30 06:48

    I don't have anything specific to add to what's been said above about the use of hydrate, but in trying to learn about it I put together a little example, so here's the work for whoever finds it helpful.

    Goal

    Serve two pages, one which uses ReactDOM.hydrate and one which uses ReactDOM.render. They will depend upon some react components written in JSX, which are loaded by <script> tags, given artificial delay (by the server) to illustrate the difference between hydrate and render.

    Basic Structure

    1. One file which has the HTML "skeleton"
    2. One file with the custom React components written in JSX
    3. One script which generates all pages for the server to use
    4. One script to run the server

    Results

    After I generate the pages and run the server, I go to 127.0.0.1 and am presented with the header hydrate, a button, and two links. I can click the button, but nothing happens. After a few moments, the document finishes loading and the button starts counting my clicks. Then I click on the "render" link. Now, the page I'm presented with has the header render and two links, but no button. After a few moments, the button appears and is immediately responsive.

    Explanation

    On the "hydrate" page, all the markup is immediately rendered, because all the necessary html is served with the page. The button is unresponsive because there are no callbacks connected yet. Once components.js finishes loading, the load event fires from the window and the callbacks are connected with hydrate.

    On the "render" page, the button markup isn't served with the page, but only injected by ReactDOM.render, so it isn't immediately visible. Note how the appearance of the page is jarringly changed by the script finally loading.

    Source

    Here is the custom react component I am using. It will be used by the server in node with react to statically render components, and it will also be loaded dynamically from the server for use in pages (this is the purpose of checking for exports and React objects at the beginning of the file).

    // components.jsx
    
    var exports = typeof(exports) == 'object' ? exports : {};
    var React = typeof(React) == 'object' ? React : require('react');
    
    function MyButton(props) {
      [click, setClick] = React.useState(0);
      function handleClick() { setClick(click + 1); }
      return (
        <button onClick={handleClick}>Clicked: {click}</button>
      );
    }
    
    exports.MyButton = MyButton;
    

    This is the script used to generate all the pages required for the server. First, babel is used to transpile components.jsx into javascript, then these components are used, along with React and ReactDOMServer, to create the actual pages. These pages are created with the fuction getPage which is exported from the file pageTemplate.js, shown next.

    // genScript.js
    
    let babel          = require('@babel/core');
    let fs             = require('fs');
    let ReactDOMServer = require('react-dom/server');
    let React          = require('react');
    let pageTemplate   = require('./pageTemplate.js');
    
    script = babel.transformFileSync(
      'components.jsx', 
      {presets : [['@babel/react']]}
    );
    
    fs.writeFileSync('components.js',script.code);
    let components = require('./components.js');
    
    hydrateHTML = pageTemplate.getPage(
      'MyButton',
      ReactDOMServer.renderToString(React.createElement(components.MyButton)),
      'hydrate'
    );
    
    renderHTML = pageTemplate.getPage(
      'MyButton',
      '',
      'render'
    );
    
    fs.writeFileSync('hydrate.html',hydrateHTML);
    fs.writeFileSync('render.html',renderHTML);
    

    This file just exports the getPage function mentioned previously.

    // pageTemplate.js
    
    exports.getPage = function(
      reactElementTag,
      reactElementString,
      reactDOMMethod
      ) {
      return `
      <!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8" />
          <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js" defer></script>
          <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" defer></script>
          <script src="./components.js" defer></script>
        </head>
        <body> 
          <h1>${ reactDOMMethod }</h1>
          <div id="react-root">${ reactElementString }</div> 
          <a href="hydrate.html">hydrate</a>
          <a href="render.html">render</a>
        </body>
        <script>
          window.addEventListener('load', (e) => {
            ReactDOM.${ reactDOMMethod }(
              React.createElement(${ reactElementTag }),
              document.getElementById('react-root')
            );
          });
        </script>
      </html>
      `;
    }
    

    Finally, the actual server

    // server.js
    
    let http = require('http');
    let fs   = require('fs');
    
    let renderPage       = fs.readFileSync('render.html');
    let hydratePage      = fs.readFileSync('hydrate.html');
    let componentsSource = fs.readFileSync('components.js');
    
    http.createServer((req, res) => {
      if (req.url == '/components.js') {
        // artificial delay
        setTimeout(() => {
        res.setHeader('Content-Type','text/javascript');
        res.end(componentsSource);
        }, 2000);
      } else if (req.url == '/render.html') {
        res.end(renderPage);
      } else {
        res.end(hydratePage);
      }
    }).listen(80,'127.0.0.1');
    
    0 讨论(0)
  • 2021-01-30 06:51

    In addition to above...

    ReactDOM.hydrate() is same as render(), but it is used to hydrate(attach event listeners) a container whose HTML contents were rendered by ReactDOMServer. React will attempt to attach event listeners to the existing markup.

    Using ReactDOM.render() to hydrate a server-rendered container is deprecated because of slowness and will be removed in React 17 so use hydrate() instead.

    0 讨论(0)
  • 2021-01-30 06:52

    The entire process of putting functionality back into the HTML that was already rendered in server side React is called hydration.

    So the process of re-rendering over the once rendered HTML is referred to as hydration.

    So if we try to hydrate our application by calling ReactDOM.render() its supposed to be done by calling ReactDOM.hydrate().

    0 讨论(0)
提交回复
热议问题