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

113
Views
Can React class components leverage custom hooks which take an argument?

One way I know how to use hooks inside a class component is to have an HOC which returns a functional component, inside of which we can call the hook and pass the prop to a class component. Look at How can I use React hooks in React classic `class` component? and answer by Joel Cox.

Now coming to the question, taking the example from https://reactjs.org/docs/hooks-custom.html#using-a-custom-hook, consider we have a hook useFriendStatus which accepts a friendID, has an effect to subscribe to that online status of that friend and cleans up on every re-render, so that for a new friendID, the hook can subscribe to the new friendID.

function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  // Effect to subscribe to online status of friendID and unsubscribe on re-render

  return isOnline;
}

Can a class component use such a hook? In the HOC pattern, we are leveraging the mount cycle of the hook. Can we leverage re-rendering of hooks in a React class component ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You really should not use your hook in a Class component at all (and definitely not in componentDidMount lifecycle method - likely throws an error), but you also cannot use your custom Hook within the useEffect of a functional component (will throw a React error stating this if you try)... part of the purpose/utility of creating is larger so that you can abstract out data fetching or other async calls from other components.

(Those components can still have other useEffects if necessary but should not contain custom Hooks)

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!