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}}
functionuseFetch(url,disableFetchOnMount=false){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]);useEffect(()=>{if(disableFetchOnMount)returnmyFetch()},[disabledFetchOnMount,myFetch])return{myFetch,data,loading,error}}
You could also add a parameter to disable the fetch on mount instead of creating a whole new hook.
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.
You would need a new hook in that case, one that returns the actual function instead of calling it inside
useEffect, something like:You could also add a parameter to disable the fetch on mount instead of creating a whole new hook.