Exemplos de HOCs
📝 Exemplo 1: withLoading
function withLoading(WrappedComponent) {
return function LoadingComponent({ isLoading, ...props }) {
if (isLoading) {
return <div>Carregando...</div>;
}
return <WrappedComponent {...props} />;
};
}
📝 Exemplo 2: withData
function withData(WrappedComponent) {
return function DataComponent(props) {
const [data, setData] = useState(null);
useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(setData);
}, []);
return <WrappedComponent data={data} {...props} />;
};
}