The DOM is the browser’s live tree representation of the HTML document. Manipulating it directly is expensive due to reflows/repaints.
React introduces the Virtual DOM (VDOM) as a lightweight in-memory representation of the DOM. On state change:
useState triggers re-render → VDOM diff → minimal DOM updates
This model improves performance and enables declarative UIs.
Semantics should guide structure, even within components. I map roles and intent to proper tags (<nav>, <main>, <section>, <article>).
Example:
function BlogPost({ title, content }) {
return (
<article>
<h2>{title}</h2>
<section>{content}</section>
</article>
);
}
I avoid div soup by treating components as semantic wrappers and using HTML5 tags when possible. This improves accessibility and SEO.
data-*, aria-*) and React props?React treats standard HTML attributes and special-purpose ones (aria-*, data-*) as props passed down to the DOM node .
<button aria-label="Close" data-testid="close-btn">
X
</button>
data-*: For testing, analytics, or behavior hooks.aria-*: For accessibility (screen reader support).Approach:
<nav>, <ul>, <li>, and ARIA roles if needed.function NavItem({ label, children }) {
return (
<li>
{label}
{children && <ul>{children.map(...</ul>}
</li>
);
}
I separate concerns (e.g., presentation vs logic) and ensure keyboard navigation and accessibility.
I use controlled components for predictable state:
const [email, setEmail] = useState("");
<input value={email} onChange={(e) => setEmail(e.target.value)} />;
For validation:
<form onSubmit={handleSubmit(onSubmit)} />
The critical rendering path is impacted by:
I optimize by:
In React:
<button onClick={handleClick}>Click me</button>
Benefits:
In vanilla JS:
React simplifies the lifecycle and performance.
I build HTML to work without JavaScript first , then enhance with React.
Example:
Use:
<noscript> fallback messaging<button type="submit">Search</button>
Enhancement adds filters, animations, etc. after hydration.
HTML is the initial render delivered to the browser in SSR:
My model:
<head> tags for meta, title, preload (via react-helmet or @vitejs/plugin-react).window, localStorage on server).<meta>, <link>) in a single-page application (SPA)?I treat metadata as part of the dynamic route context . For SPAs:
react-helmet or @tanstack/router to declaratively set <title>, <meta>, etc.<Helmet>
<title>Product Page</title>
<meta name="description" content="Details about product" />
</Helmet>
For PWAs, also manage <link rel="manifest">, icons, and preload hints.