← React Intermediate Questions - Set 9 Q88

What is a “higher-order component” (HOC) in React?

React Intermediate Questions - Set 9

Example

const withTimer = (C) => (props) => {
  const [t, setT] = useState(0);
  useEffect(() => { const i = setInterval(() => setT(x => x + 1), 1000); return () => clearInterval(i); }, []);
  return <C {...props} elapsed={t} />;
};

What to notice

← Back to the question list