Answers for "redux action to hit api and assign data in stateless component"

0

redux action to hit api and assign data in stateless component

export function fetchProducts() {
  return dispatch => {
    dispatch(fetchProductsBegin());
    return fetch("/products")
      .then(handleErrors)
      .then(res => res.json())
      .then(json => {
        dispatch(fetchProductsSuccess(json.products));
        return json.products;
      })
      .catch(error => dispatch(fetchProductsFailure(error)));
  };
}

// Handle HTTP errors since fetch won't.
function handleErrors(response) {
  if (!response.ok) {
    throw Error(response.statusText);
  }
  return response;
}
Posted by: Guest on April-24-2020

Code answers related to "redux action to hit api and assign data in stateless component"

Code answers related to "Javascript"

Browse Popular Code Answers by Language