Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!