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

122
Vistas
Can I extract a function to a utils file and then use it in a React component while passing a function from the component to it?

I have a function getFontColor(), that I would like to extract to a utils folder, and call it in a component. This function though, uses a function that is inside my component (getBgColor()). How do I pass getBgColor to the util function and how to I call the util function in my component?

Util function I want to extract:

  export const getFontColor = () => {
    const bgColor = getBgColor();   
    return something;
  };

Function getBgColor() which is inside the component:

  const getBgColor = () => {
    return something
  };

Calling the util function:

 <StyledTitle color={getFontColor}>
   {node.label}
 </StyledTitle>

How can I communicate those? The main issue, is how do I pass the getBgColor to the external function?

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

0

Just pass your getBgColor-function to your utils-function. In Javascript functions are first class objects, so they behave like any other object, such as an int or a list. That means that you can use functions as arguments to other functions.

// utils.js
export const getFontColor = async (props) => {
  const fontColor = await getFontColorAsync(props);
  return fontColor;
}

const getFontColorAsync = async ({ getBgColor }) => {
  const bgColor = getBgColor();  
  return something;
}
//component.jsx
import { getFontColor } from "./utils.js"

function Component() {
  const getBgColor = () => {
    return something;
  };

  const fontColor = getFontColor({ getBgColor });

  return (
    <StyledTitle color={fontColor}>
      {node.label}
    </StyledTitle>
  );
}
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