You would need a new hook in that case, one that returns the actual function instead of calling it inside useEffect, something like:
functionuseFetch(url){const[data,setData]=useState(null);const[loading,setLoading]=useState(null);const[error,setError]=useState(null);constmyFetch=useCallback(()=>{setLoading('loading...')setData(null);setError(null);axios.get(url).then(res=>{setLoading(false);// checking for multiple responses for more flexibility // with the url we send in.res.data.content&&setData(res.data.content);res.content&&setData(res.content);}).catch(err=>{setLoading(false)setError('An error occurred. Awkward..')})},[url]);return{myFetch,data,loading,error}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
How can we call the custom hook on button click event?
You would need a new hook in that case, one that returns the actual function instead of calling it inside
useEffect
, something like: