Skip to main content
InterviewHubPRO
Section Overview
Q51

Question 51 of 407

General

13%
React Core & Hooks (407 Qs)•General

Do Hooks replace render props and higher order components?

mid LevelCode ExampleComponentsHooks

Both render props and higher-order components render only a single child but in most of the cases Hooks are a simpler way to serve this by reducing nesting in your tree.

Practical Code Example

jsx
// Custom Hooks replace verbose Render Props and HOCs:
// Modern Custom Hook approach:
import { useState, useEffect } from 'react';

export function useWindowSize() {
  const [size, setSize] = useState({ width: window.innerWidth, height: window.innerHeight });

  useEffect(() => {
    const handleResize = () => setSize({ width: window.innerWidth, height: window.innerHeight });
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);

  return size;
}