Approach : Use a framework like Next.js , which abstracts the SSR setup.
Concept :
This improves initial load performance , SEO , and time-to-interactive .
Next.js Example :
// pages/index.tsx
export default function Home({ user }) {
return <div>Hello, {user.name}</div>;
}
export async function getServerSideProps() {
const res = await fetch("https://api.example.com/user");
const user = await res.json();
return { props: { user } };
}
Custom SSR (using ReactDOMServer):
const html = ReactDOMServer.renderToString(<App />);
When to use : Public-facing pages that need SEO or fast first paint (e.g., landing pages, e-commerce).
Controlled : Form state is managed by React via useState.
const [value, setValue] = useState("");
<input value={value} onChange={(e) => setValue(e.target.value)} />;
Uncontrolled : Form state is handled by the DOM via a ref.
const inputRef = useRef();
<input ref={inputRef} />;
When to use :
Concept :
Error boundaries catch rendering , lifecycle , or constructor errors in child components — not event handlers or async code.
Example :
class ErrorBoundary extends React.Component {
state = { hasError: false };
static getDerivedStateFromError() {
return { hasError: true };
}
componentDidCatch(error, info) {
// Log error to monitoring service
console.error(error, info);
}
render() {
return this.state.hasError ? (
<h1>Something went wrong.</h1>
) : (
this.props.children
);
}
}
// Usage
<ErrorBoundary>
<MyComponent />
</ErrorBoundary>;
When to use : Around major feature components, routes, or dynamic imports.
Key Practices :
/features/user
/features/dashboard
/shared/components
Event Delegation : A single handler (e.g., on document) listens for events from many child elements by leveraging event bubbling.
React’s Synthetic Events :
React bubbles events through the virtual DOM tree instead of the actual DOM.
Example :
function App() {
return <button onClick={() => console.log("clicked")}>Click Me</button>;
}
Under the hood, this onClick is handled via a global listener on the root, improving performance and memory efficiency .