Criando HOCs
📝 Estrutura Básica
function withFeature(WrappedComponent) {
return function EnhancedComponent(props) {
// Lógica adicional aqui
return <WrappedComponent {...props} />;
};
}
// Uso
const EnhancedButton = withFeature(Button);
💡 Exemplo: HOC com Autenticação
function withAuth(WrappedComponent) {
return function AuthenticatedComponent(props) {
const [isAuthenticated, setIsAuthenticated] = useState(false);
if (!isAuthenticated) {
return <div>Por favor, faça login</div>;
}
return <WrappedComponent {...props} />;
};
}
const ProtectedPage = withAuth(Page);