Advanced Level React Interview Questions & Answers – Set 1
5 minute
Advanced Level React Interview Questions & Answers – Set 1
1. What are Server Components in React 18? How do they differ from Client Components?
Server Components: These run on the server, not in the browser.
They don’t send JavaScript to the client, which reduces bundle size and improves performance.
Client Components: These run in the browser, handle interactions like clicks, inputs, animations, etc.
Key Difference:
Server Components cannot use hooks like useState or useEffect because they don’t run in the browser. They are mainly for rendering static or data-heavy UI.
Example:
// Server Component (fetching data from DB)
export default async function Products() {
const data = await getProductsFromDB();
return (
<ul>
{data.map(p => <li key={p.id}>{p.name}</li>)}
</ul>
);
}
// Client Component (handling clicks)
"use client";
export default function Button() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(c => c + 1)}>Clicked {count}</button>
}
2. What are Compound Components in React, and why are they useful?
Compound Components let you build a parent component with flexible children that share implicit state.
They are useful when you want a reusable, customizable component with controlled parts.
Example (Tabs):
function Tabs({ children }) {
const [active, setActive] = React.useState(0);
return React.Children.map(children, (child, index) =>
React.cloneElement(child, { active, setActive, index })
);
}
function Tab({ index, setActive, active, children }) {
return (
<buttonstyle={{ fontWeight: active === index ? "bold" : "normal" }}
onClick={() => setActive(index)}
>
{children}
</button>
);
}
function TabPanel({ index, active, children }) {
return active === index ? <div>{children}</div> : null;
}
// Usage
<Tabs>
<Tab>Home</Tab>
<Tab>Profile</Tab>
<TabPanel>Home Content</TabPanel>
<TabPanel>Profile Content</TabPanel>
</Tabs>
3. How does the Render Props pattern differ from HOCs? Which one is preferable?
Render Props: Pass a function as a child to share logic.
HOC (Higher Order Component): Wrap a component to inject extra props.
Today, Hooks are preferred because they are simpler and more readable.