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

100
Views
Use React Hooks in class object

I know it is not possible to use Hooks in Class Components, that is not what I am trying to achieve.

I have a Context I want to access in a service class.

/* File MyComponent.tsx */
function MyComponent() {
  MyService.foo();
  return (
    <h1>Anything</h1>
  )
}

/* File MyService.tsx */
class MyService {
 foo() {
   const [dispatch] = React.useContext(MyContext);
   dispatch({type: 'myaction'});
 }
}
export default new MyService()

I Have already found a workaround by passing the dispatch callback from the component to the service method. But this is pretty ugly and tedious.

function useMyServiceFoo(params) {
  const [dispatch] = React.useContext(MyContext);
  MyService.foo(dispatch, params);
}

As this is working I wonder why it is not when I call useContext in the class methods ? Is there a way I can make it works ?

about 4 years ago · Juan Pablo Isaza
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!