Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
What are the pros and cons of the HOC pattern vs React Hooks to inject props/dependencies?

Suppose we have a HOC withWidth and a hook useWidth which both injects the current window width as a prop in a component.

withWidth:

const MyFunctionComponent = withWidth(({width}) => {
  return width === 'mobile'
    ? // render mobile version
    : // render desktop version
});

useWidth:

const MyFunctionComponent = () => {
  const width = useWidth();

  return width === 'mobile'
    ? // render mobile version
    : // render desktop version
};

What are the pros and cons both patterns? When should I prefer the one over the other?

The withWidth HOC solution isn't better at a Dependency Inversion Principle perspective? It's easier to mock the width dependency and to write tests, and the component itself doesn't have to handle the width creation.

And if it is, why we prioritize the usage of hooks? At least I often see this approach, prioritizing the usage of hooks instead of HOCs when possible.

I'm still learning about concepts of Dependency Inversion and stuff. These concepts often are examplified using Java, so transposing to JS and React world is quite difficult to me.

about 4 years ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda