Implement a Higher Order Component (HOC) withLoading that displays a loading spinner.
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
import React from 'react';
function withLoading(Component) {
return function WithLoadingComponent({ isLoading, ...props }) {
if (isLoading) return <p>Loading...</p>;
return <Component {...props} />;
};
}
// Usage:
// const UserListWithLoading = withLoading(UserList);
// <UserListWithLoading isLoading={true} users={[]} />