React Component Design Interview Guide
What is a React Component?
Section titled “What is a React Component?”A React component is a reusable piece of UI that encapsulates its own structure, logic, and styling.
function Button() { return <button>Click Me</button>;}Senior-Level Perspective
Section titled “Senior-Level Perspective”A component is the fundamental building block of React applications. It promotes reusability, maintainability, and separation of concerns by encapsulating UI and behavior into independent units.
Types of React Components
Section titled “Types of React Components”Functional Components
Section titled “Functional Components”function Welcome() { return <h1>Hello</h1>;}Class Components
Section titled “Class Components”class Welcome extends React.Component { render() { return <h1>Hello</h1>; }}Modern React primarily uses functional components with Hooks because they are simpler and provide all lifecycle capabilities.
Characteristics of Good Component Design
Section titled “Characteristics of Good Component Design”A well-designed component should be:
- Reusable
- Single Responsibility
- Maintainable
- Testable
- Scalable
Example
Section titled “Example”Bad
UserDashboardHandles:
- API calls
- Rendering
- Validation
- Business logic
Good
flowchart TD
A[UserDashboard] --> B[UserProfile]
A --> C[UserStats]
A --> D[UserActions]
Each component has one responsibility.
Single Responsibility Principle (SRP)
Section titled “Single Responsibility Principle (SRP)”A component should have only one reason to change.
function UserComponent() { // fetch user // display user // update user}UserListUserDetailsUserFormApplying SRP improves reusability, testing, and reduces coupling.
When Should You Create a New Component?
Section titled “When Should You Create a New Component?”Create a new component when:
- UI repeats
- Logic repeats
- Component becomes large
- Reusability is needed
Instead of:
<Card1 /><Card2 /><Card3 />Use:
<Card title="A" /><Card title="B" /><Card title="C" />Smart vs Dumb Components
Section titled “Smart vs Dumb Components”Smart (Container)
Section titled “Smart (Container)”- API calls
- State management
- Business logic
Dumb (Presentational)
Section titled “Dumb (Presentational)”- UI rendering only
flowchart TD
A[UserContainer] --> B[UserCard]
Prop Drilling
Section titled “Prop Drilling”Passing props through multiple levels.
flowchart TD
A[App] --> B[Parent]
B --> C[Child]
C --> D[GrandChild]
Example:
<App user={user} />Solutions:
- Context API
- Redux
- Zustand
Designing Reusable Components
Section titled “Designing Reusable Components”Use configurable props.
<Button text="Save" variant="primary" />Avoid hardcoding.
<button>{text}</button>Component Composition
Section titled “Component Composition”Composition combines smaller components into larger ones.
<Card> <Header /> <Body /> <Footer /></Card>Benefits:
- Reusability
- Flexibility
- Maintainability
Composition vs Inheritance
Section titled “Composition vs Inheritance”React recommends composition over inheritance.
<Card> <UserProfile /></Card>Children Prop
Section titled “Children Prop”function Card({ children }) { return <div>{children}</div>;}Usage:
<Card> <h1>Hello</h1></Card>Designing Scalable Forms
Section titled “Designing Scalable Forms”Separate concerns:
UserForm- FormFields- Validation- API ServiceRecommended libraries:
- React Hook Form
- Formik
Sharing Logic
Section titled “Sharing Logic”Use custom hooks.
function useFetch(url) { // fetch logic}
const data = useFetch("/users");Avoiding Unnecessary Re-renders
Section titled “Avoiding Unnecessary Re-renders”React.memo
Section titled “React.memo”const UserCard = React.memo(({ user }) => { return <div>{user.name}</div>;});useMemo
Section titled “useMemo”const result = useMemo(() => expensiveCalc(), []);useCallback
Section titled “useCallback”const handleClick = useCallback(() => {}, []);Structuring a Large React Application
Section titled “Structuring a Large React Application”Traditional:
src- components- pages- hooks- services- contexts- utils- store- routesFeature-based:
features- users - components - hooks - api - pages- products- ordersControlled vs Uncontrolled Components
Section titled “Controlled vs Uncontrolled Components”Component design decisions around forms often come back to this distinction. See the React Interview Study Guide for the controlled vs uncontrolled comparison and code examples.
Designing a Reusable Modal
Section titled “Designing a Reusable Modal”<Modal isOpen={isOpen} onClose={closeModal}> <UserForm /></Modal>Design considerations:
- Accept children
- Configurable props
- Independent state
- Reusable
Designing a Reusable Table
Section titled “Designing a Reusable Table”<Table data={users} columns={columns}/>const columns = [ { key: "name", title: "Name" }, { key: "email", title: "Email" }];Benefits:
- Dynamic rendering
- Reusable
- Customizable
Sibling Component Communication
Section titled “Sibling Component Communication”Lift state to the common parent.
flowchart TD
P[Parent]
P --> A[Child A]
P --> B[Child B]
Alternatives:
- Context API
- Redux
- Zustand
Designing a Reusable Button
Section titled “Designing a Reusable Button”<Button variant="primary" size="large" loading={true} disabled={false} onClick={handleClick}> Save</Button>Consider:
- Accessibility (ARIA)
- Loading state
- Disabled state
- Theme support
- Dark mode
- Responsive design
- Testability
Frequently Asked Senior Interview Questions
Section titled “Frequently Asked Senior Interview Questions”- How would you design a reusable table component?
- How would you build a design system in React?
- Smart vs Dumb components?
- Composition vs Inheritance?
- How do you avoid prop drilling?
- How do you structure a large React application?
- How do you design scalable forms?
- How do you share business logic across components?
- How would you design reusable modals, dropdowns, and buttons?
- What are the common mistakes in component design and how do you avoid them?
Key Takeaways
Section titled “Key Takeaways”- Build small, reusable components with a single responsibility.
- Prefer composition over inheritance.
- Share logic using custom hooks rather than duplicating code.
- Design configurable components using props and
children. - Organize large applications using a feature-based folder structure.
- Optimize rendering with
React.memo,useMemo, anduseCallback.