Skip to content Skip to sidebar Skip to footer

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 c

Solution 1:

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)

Post a Comment for "Can React Class Components Leverage Custom Hooks Which Take An Argument?"