A higher-order component is a function that receives a component and returns a new component with shared behavior. Implement withLoading so the enhanced component chooses between a loading branch and the wrapped content. The wrapper must preserve ordinary props, children, refs, and a useful name for React DevTools.
function withLoading(
WrappedComponent,
LoadingComponent = null,
): React.ForwardRefExoticComponent;
// Reserved enhanced props:
// isLoading?: boolean // defaults to false
// loadingProps?: object // defaults to {}
const LoadableProfile = withLoading(Profile, Spinner);
<LoadableProfile user={ada} isLoading={false} />;
// Renders <Profile user={ada} />.
<LoadableProfile
user={ada}
isLoading={true}
loadingProps={{ label: 'Loading profile' }}
/>;
// Renders <Spinner label="Loading profile" /> without mounting Profile.
isLoading === true selects the loading component; omitted or other values select the wrapped component.isLoading or loadingProps to the wrapped component, and pass only loadingProps to the loading component.React.forwardRef so the caller can reach the wrapped component's DOM node or imperative handle.displayName from the wrapped component's displayName, then its name, then Component.defaultProps, memoize, fetch data, or create the HOC during render.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
A higher-order component is a function that receives a component and returns a new component with shared behavior. Implement withLoading so the enhanced component chooses between a loading branch and the wrapped content. The wrapper must preserve ordinary props, children, refs, and a useful name for React DevTools.
function withLoading(
WrappedComponent,
LoadingComponent = null,
): React.ForwardRefExoticComponent;
// Reserved enhanced props:
// isLoading?: boolean // defaults to false
// loadingProps?: object // defaults to {}
const LoadableProfile = withLoading(Profile, Spinner);
<LoadableProfile user={ada} isLoading={false} />;
// Renders <Profile user={ada} />.
<LoadableProfile
user={ada}
isLoading={true}
loadingProps={{ label: 'Loading profile' }}
/>;
// Renders <Spinner label="Loading profile" /> without mounting Profile.
isLoading === true selects the loading component; omitted or other values select the wrapped component.isLoading or loadingProps to the wrapped component, and pass only loadingProps to the loading component.React.forwardRef so the caller can reach the wrapped component's DOM node or imperative handle.displayName from the wrapped component's displayName, then its name, then Component.defaultProps, memoize, fetch data, or create the HOC during render.The full solution is part of Premium
Walkthrough, edge cases, complexity notes, and the runnable editor unlock with a Premium subscription.
Submissions are part of Premium
Unlock community code, comments, reactions, and framework-specific approaches.
Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.