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

163
Vistas
React call functions on renderless component

I need to have a component for handling settings, this component (called Settings) stores state using useState(), for example the primary color.

I need to create a single instance of this component and make it available to every component in the app. Luckily, I already pass down a state dict to every component (I'm very unsure if this is the correct way to achieve that btw), so I can just include this Settings constant.

My problem is that I don't know how to create the component for this purpose, so that I can call its functions and pass it to children.

Here is roughly what my Settings component looks like:

const Settings = (props) => {
    const [primaryColor, setPrimaryColor] = useState("")
    const getColorTheme = (): string => {
        return primaryColor
    }
    const setColorTheme = (color: string): void => {
        setPrimaryColor(color)
    }
    return null
}


export default Settings

Then I would like to be able to do something like this somewhere else in the app:

const App = () => {
    const settings = <Settings />
    return (
        <div style={{ color: settings.getColorTheme() }}></div>
    )
}

Bear in mind that I'm completely new to react, so my approach is probably completely wrong.

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

0

You can use a custom Higher Order Component(HOC) for this purpose, which is easier than creating a context(even thougn context is also a HOC). A HOC takes a component and returns a new component. You can send any data from your HOC to the received component.

const withSettings = (Component) => {
  const [settings, setSettings] = useState({})
  // ...
  // ...
  <Component {...props} settings={settings}/>
);

And you can use it like this:

const Component = ({ settings }) => {
   ...your settings UI
}
export default SettingsUI = withSettings(Component);

You can read more about HOCs in the official react documentation

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