01 / 05

List different ways in which we can build reusable code and promote code sharing

There are different ways in which we can build shareable code in react:

  1. 1

    Hooks

  2. 2

    Higher Order Components

  3. 3

    Render Props

  4. 4

    Component composition

Difficulty: 5/10
Topics: component composition, custom hooks, shared libraries

Scenario Questions

0-2 years experience
  1. 1

    How would you extract a button component so it can be used on two different pages without duplicating code?

  2. 2

    If you create a custom hook for data fetching, where do you place the file and how do you import it in a component?

  3. 3

    What happens if you import the same component using two different relative paths in a small React app?

2-5 years experience
  1. 1

    We need a modal that appears in several places but each usage requires slightly different styling. How would you design it for reuse and what pitfalls might you watch for?

  2. 2

    After moving a shared utility into a new folder, the production build started throwing ‘module not found’ errors. Walk me through how you’d debug this.

  3. 3

    Why did wrapping a list item with a higher‑order component for authentication cause unexpected re‑renders, and how would you fix it?

5-8 years experience
  1. 1

    Our organization wants a shared component library across multiple React apps with different theming needs. How would you structure the library, handle versioning, and keep bundle impact low?

  2. 2

    Introducing a custom‑hook library caused a memory leak in a high‑frequency component. How would you identify the leak and resolve it?

  3. 3

    Compare using a monorepo versus separate repos for sharing UI components across teams. What are the trade‑offs in terms of CI, dependency management, and team autonomy?

8+ years experience
  1. 1

    We have legacy codebases using class components and a new codebase using functional components with hooks. Outline a migration strategy for shared utilities and components that minimizes disruption.

  2. 2

    Design a governance model for a company‑wide React component library that balances product‑team autonomy with consistency and long‑term maintainability.

  3. 3

    A critical security vulnerability is discovered in a shared npm package used by dozens of services. How would you coordinate the fix and rollout across the organization?

Follow-up Questions

  • What trade‑offs did you consider when choosing that pattern?
  • How would you test that the reusable piece works in all contexts?
  • What impact does this have on bundle size or load performance?