Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React custom hook refetch data

const useGetFetch(url, token, user, parser) => {
  const [state, dispatch] = useReducer(fetchReducer, initialState)
  // calling refetch will force a rerender on the hook
  const [shouldRefetch, refetch] = useState({}); 

  useEffect (() => {
    if(user){
      fetch(...)
      .then((data)=> data.json())
      .then(dispatch(...)) // setting the state here
      .catch((error) => dispatch(...)) // set error state
    }
  }, [user, shouldRefetch]); // note the dep array

 // returning the refetch functions so we can call the refetch() on modal close.
 return [state.response, state.loading, state.error, refetch];
}

To trigger

refetch({});
Comment

PREVIOUS NEXT
Code Example
Javascript :: what does bang at the end of a statement mean for in typescript 
Javascript :: node equivalent of bash exec 
Javascript :: switch is not exported from react-router-dom 
Javascript :: how to ask for leave in js 
Javascript :: http response in json format usin gjava 
Javascript :: screenfull angular example 
Javascript :: Scrub punctuation character 
Javascript :: reply nodejs terminal 
Javascript :: detect finishing write react input 
Javascript :: globalevariable reactjs 
Javascript :: bug in javascript 
Javascript :: internation number 
Javascript :: remove property from query string javascript 
Javascript :: how to style elements that had ben added with inner html js 
Javascript :: get current month first date and last date in javascript 
Javascript :: escaping less than great than signs in react 
Javascript :: how to reload page with router next js 
Javascript :: Search products from an array by keywords in javascript 
Javascript :: lucastools version info getter 
Javascript :: numbers Math 
Javascript :: js what does the vertical line symbol do 
Javascript :: reduceat 
Javascript :: javascript make variable 
Javascript :: comparing oblects 
Javascript :: angular show text in template before data loads 
Javascript :: Uncaught TypeError: $(...).steps is not a function 
Javascript :: ingore render on refresh page 
Javascript :: AsyncStorage getAllKeys seperately 
Javascript :: key index split 
Javascript :: provider._web3Provide.sendAsync as any 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =