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

248
Vistas
React Hook most proper ways to refactor resusable functions and effects

let's consider two hook components:

export function HookCompA({a,b,c,d}){
  ...
  useEffect(...,[x])

  useEffect(...,[y])

  functionA{...};
  functionB{...};
  functionC{...};
  functionD{...};
...
}

export function HookCompB({a,b,c,e}){
  ...
  useEffect(...,[x])

  useEffect(...,[w])

  functionA{...};
  functionB{...};
  functionC{...};
  functionE{...};
  ...
}

Despite being so similar, due to the nature of the context, we have to consider these hooks as different components, however, there is a lot of duplicated code to refactor and they even share the same code for some effects.

Due to my experience with OOP, it is hard to imagine how to solve this issue without a callback to inheritance, which is not what hooks is all about. I neither think that just exporting the functions in another file and adding them to both hooks is the best idea.

I wish to know the best practice for reusing code in hooks, specially for effects.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I wish to know the best practice for reusing code in hooks, specially for effects.

This is rather subjective/opinionated question, so I'll attempt to answer objectively.

React hooks are just plain old Javascript functions with some special rules applied against them in how and where they can be called.

Despite being so similar, due to the nature of the context, we have to consider these hooks as different components, however, there is a lot of duplicated code to refactor and they even share the same code for some effects.

React hooks were designed with composability/reusability code in mind.

Motivation

With Hooks, you can extract stateful logic from a component so it can be tested independently and reused. Hooks allow you to reuse stateful logic without changing your component hierarchy. This makes it easy to share Hooks among many components or with the community.

This means exactly as you describe, centralizing the "common" functions in, say a src/utils directory where they can be testing in isolation and proven to work, and exported for composition into other units, like custom React hooks. Even the functions used in the useEffect and other base React hooks can be centralized/organized.

import {
  functionA,
  functionB,
  functionC,
  functionD,
  functionE,
  functionF,
  functionG,
  functionH,
} from '../utils';

export useBaseHook = (a,b,c) => {
  useEffect(() => {
    functionG(a, b);
    functionH(c);
  }, [a, b, c]);
};

export function useHookCompA({ a, b, c, d }) {
  ...
  useEffect(...,[x]);
  useEffect(...,[y]);

  useBaseHook(d, c, a);

  functionA{...};
  functionB{...};
  functionC{...};
  functionD{...};
  ...
}

export function useHookCompB({ a, b, c, e }) {
  ...
  useEffect(...,[x]);
  useEffect(...,[w]);

  functionA{...};
  functionB{...};
  functionC{...};
  functionE{...};
  ...
}
about 4 years ago · Juan Pablo Isaza Denunciar
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