Skip to the solution
EasyFrontend7 min read

What is the role of react-dom?

956 views

The problem

Question presented to candidate: "You have got react and react-dom in your package.json. What is the difference β€” why are they two separate packages, and what does react-dom actually do?"

What a strong answer should cover:

  • react is the platform-agnostic core: components, Hooks, and reconciliation. It has no idea what a <div> is.
  • react-dom is the renderer that applies React's output to a real browser DOM.
  • The three entry points: react-dom/client (createRoot / hydrateRoot), react-dom/server (renderToString and the streaming APIs), and the top-level react-dom utilities.
  • Why the split exists: the same component code can target other renderers β€” React Native, react-three-fiber, react-pdf.
  • ReactDOM.render, ReactDOM.hydrate, and ReactDOM.findDOMNode were deprecated in React 18 and removed in React 19.
  • Bonus signal: useFormStatus ships from react-dom, not react β€” a detail most candidates get backwards.

Clarifying questions expected:

  • "Which React version are we targeting?" β€” the answer changes for 19 vs. 17.
  • "Do you want the client story, the server story, or both?"

Code / implementation expected: Optional. A short createPortal snippet is the strongest concrete demonstration of a react-dom-specific capability.

  • react-dom
  • dom
  • rendering
  • architecture

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 9 sections

Try it in the playground

Open any example in the playground to run and change it.

ReactcreatePortal β€” a modal that escapes its parent DOM node
import { useState } from "react";
import { createPortal } from "react-dom";

// The portal target. In a real app this is a <div id="modal-root"> that lives
// in index.html as a sibling of the app root.
function useModalHost() {
  const host = document.getElementById("modal-root") ?? (() => {
    const el = document.createElement("div");
    el.id = "modal-root";
    document.body.appendChild(el);
    return el;
  })();
  return host;
}

function Modal({ onClose }) {
  const host = useModalHost();
  return createPortal(
    <div style={{ position: "fixed", inset: 0, background: "rgba(0,0,0,.5)" }}>
      <div style={{ background: "#fff", margin: "20vh auto", padding: 24, width: 280, borderRadius: 8 }}>
        <p style={{ marginTop: 0 }}>I render into #modal-root, not into my parent.</p>
        <button onClick={onClose}>Close</button>
      </div>
    </div>,
    host,
  );
}

export default function App() {
  const [open, setOpen] = useState(false);
  const [clicks, setClicks] = useState(0);

  return (
    // This handler still fires for clicks inside the portal: events bubble
    // through the REACT tree, not the DOM tree.
    <div onClick={() => setClicks((c) => c + 1)} style={{ padding: 24, fontFamily: "system-ui" }}>
      <p>Clicks seen by the logical parent: <strong>{clicks}</strong></p>
      <button onClick={() => setOpen(true)}>Open modal</button>
      {open && <Modal onClose={() => setOpen(false)} />}
      <p style={{ color: "#666", fontSize: 13 }}>
        Open the modal and click Close β€” the parent counter still increments,
        even though the modal DOM lives outside this div.
      </p>
    </div>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 6 of 119 in React.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All React.js questions
Up nextEasy

What are React Developer Tools and how do you use them?

Continue