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

349
Vistas
How do I use other React hooks inside a React useMemo hook function?

I was thinking of optimizing some code as the results can be cached since it only needs to iterate through the data once. In my case it's determining the colors and font styles for a specific set of components for Native Base.

Presently I useTheme() to get the theme data. So I thought I can simply go from

function useTextStyle(component: string) : TextStyle {
  const theme = useTheme();
  const colorMode = useColorMode();
  return computeTextStyles(component, colorMode);
}
function useTextStyles() : StyleSheet.NamedStyle<{header1:TextStyle,body:TextStyle}> {
  return {
    header1: useTextStyle('Heading');
    body: useTextStyle('Text');
  }
}

to

function useTextStyles() {
  const colorMode = useColorMode();
  return useMemo(()=> ({
    header1: useTextStyle('Heading');
    body: useTextStyle('Text');
  }), [colorMode])
}

But that gives a warning saying I shouldn't use hooks in useMemo.

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

0

(Without being preachy I promise) I would first ask whether this is a needed adjustment - unless theres a noticeable performance dip I wouldn't bother. Ultimately none of this should change unless the colorMode is changing by the looks of things, and it can be a bad thing when things are being over-optimised (useMemo does have a cost associated with calling it, which may or may not outweigh the benefit of the memoization). Best to use the chrome debugger to see if it actually changes things after, if not faster then it's probably best left unoptimised for the sake of the next develop trying to figure out whats going on).

However! If you deem it is necessary, then the problem is in the useTextStyle within the memo portion.

I would personally be more inclined to write it as:

function useTextStyles() : StyleSheet.NamedStyle<{header1:TextStyle,body:TextStyle}> {
  // const theme = useTheme(); // Is this even used?
  const colorMode = useColorMode();

  return useMemo(()=> ({
    header1: computeTextStyles('Heading', colorMode);
    body: computeTextStyles('Body', colorMode);
  }), [colorMode])
}

Either way you still need to call the compute functions on all of the entries, and this way you are only just calling the useColorMode once and passing the value along, and the only dependency is to be the colorMode, (and computeTextStyle)

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