1. import * as React from 'react';
    
  2. import {use, Suspense, useState, startTransition} from 'react';
    
  3. import ReactDOM from 'react-dom/client';
    
  4. import {createFromFetch, encodeReply} from 'react-server-dom-webpack/client';
    
  5. 
    
  6. // TODO: This should be a dependency of the App but we haven't implemented CSS in Node yet.
    
  7. import './style.css';
    
  8. 
    
  9. let updateRoot;
    
  10. async function callServer(id, args) {
    
  11.   const response = fetch('/', {
    
  12.     method: 'POST',
    
  13.     headers: {
    
  14.       Accept: 'text/x-component',
    
  15.       'rsc-action': id,
    
  16.     },
    
  17.     body: await encodeReply(args),
    
  18.   });
    
  19.   const {returnValue, root} = await createFromFetch(response, {callServer});
    
  20.   // Refresh the tree with the new RSC payload.
    
  21.   startTransition(() => {
    
  22.     updateRoot(root);
    
  23.   });
    
  24.   return returnValue;
    
  25. }
    
  26. 
    
  27. function Shell({data}) {
    
  28.   const [root, setRoot] = useState(data);
    
  29.   updateRoot = setRoot;
    
  30.   return root;
    
  31. }
    
  32. 
    
  33. async function hydrateApp() {
    
  34.   const {root, returnValue, formState} = await createFromFetch(
    
  35.     fetch('/', {
    
  36.       headers: {
    
  37.         Accept: 'text/x-component',
    
  38.       },
    
  39.     }),
    
  40.     {
    
  41.       callServer,
    
  42.     }
    
  43.   );
    
  44. 
    
  45.   ReactDOM.hydrateRoot(document, <Shell data={root} />, {
    
  46.     // TODO: This part doesn't actually work because the server only returns
    
  47.     // form state during the request that submitted the form. Which means it
    
  48.     // the state needs to be transported as part of the HTML stream. We intend
    
  49.     // to add a feature to Fizz for this, but for now it's up to the
    
  50.     // metaframework to implement correctly.
    
  51.     formState: formState,
    
  52.   });
    
  53. }
    
  54. 
    
  55. // Remove this line to simulate MPA behavior
    
  56. hydrateApp();