You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's one attempt I made at making a hook. Works quite nicely. I think this could be quite a nice addition to this library.
import_from'lodash-es';import{useCallback,useEffect}from'react';import{useDispatch,useSelector}from'react-redux';import{getRequestInfo,invalidateRequests}from'redux-bees';exportconstuseQueryWithInitialFetch=(apiCall,args)=>{constqueryResult=useQuery(apiCall,args);const[,,fetch]=queryResult;useEffect(()=>{fetch({invalidate: false});// eslint-disable-next-line react-hooks/exhaustive-deps},[]);returnqueryResult;};exportconstuseQuery=(apiCall,args)=>{constdispatch=useDispatch();constrequestInfo=useSelector(state=>getRequestInfo(state,apiCall,[args]),_.isEqual);constfetch=useCallback((options={})=>{const{ invalidate =false}=options;return(async()=>{if(invalidate){awaitdispatch(invalidateRequests(apiCall,args));}returndispatch(apiCall(args));})();},// stringify the args so they can be trivially compared. Two args that are effectively identical shouldn't// cause a fetch re-creation, otherwise two { } objects would actually cause a diff here and indicate to the// caller that there is a new fetch to execute on...// eslint-disable-next-line react-hooks/exhaustive-deps[dispatch,apiCall,JSON.stringify(args)]);constinvalidateAll=useCallback(()=>dispatch(invalidateRequests(apiCall)),[dispatch,apiCall]);return[requestInfo.result,{ ..._.omit(requestInfo,'results')},fetch,invalidateAll];};
use case:
const[terms,setTerms]=useState('');const[data,status,fetch,invalidateAll]=useQuery(api.theCall,{'filter[name]': terms});// this gets updated when terms changes because fetch gets re-createduseEffect(()=>{fetch();},[fetch]);
The text was updated successfully, but these errors were encountered:
Here's one attempt I made at making a hook. Works quite nicely. I think this could be quite a nice addition to this library.
use case:
The text was updated successfully, but these errors were encountered: