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 ?