// --- HOC version ---
function withLoading(Component) {
return function WithLoadingWrapper({ isLoading, ...props }) {
if (isLoading) {
return
Loading...
;
}
return ;
};
}
function UserProfile({ name }) {
return {name}
;
}
const UserProfileWithLoading = withLoading(UserProfile);
function AppWithHOC() {
return ;
}
// --- Custom hook version ---
import { useState } from "react";
function useLoading(initialValue = true) {
const [isLoading, setIsLoading] = useState(initialValue);
return { isLoading, setIsLoading };
}
function UserProfilePage() {
const { isLoading } = useLoading(false);
if (isLoading) {
return Loading...
;
}
return Philip
;
}
function AppWithHook() {
return ;
}
/*
Notes:
- The HOC version adds an extra wrapper component
(WithLoadingWrapper) that shows up as its own layer in React
DevTools, and requires every wrapped component to accept an
isLoading prop it didn't ask for directly.
- The custom hook version has no extra wrapper component at all —
UserProfilePage simply calls useLoading() and handles its own
conditional rendering inline, exactly the kind of simplification
that made HOCs mostly unnecessary once hooks existed.
*/