Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use fetch not depends on fetch function and better error handling #6217

Closed
wants to merge 1 commit into from

Conversation

ElTupac
Copy link
Contributor

@ElTupac ElTupac commented May 10, 2024

Based on the issue #6215, that use-fetch hook throws error and crash whole application. Proposal is a better error handling model, and also to not depend the flow in the fetch function, leaving developer to use whatever promise handler he wants. Example of use:

  • Component level:
const { data, error, loading, fetch, abort } = useFetch(getInstallmentsList);

  useEffect(() => {
    fetch();
  }, []);

Service level:

export const getInstallmentsList = async () => {
  const { data } = await axios.get<PaymentListEntity[]>(
    "https://xchjpacsyl.execute-api.us-east-1.amazonaws.com/prod/bill_order/v1/installments"
  );

  return data;
};

If the service function throws an error, it will be catched in the use-fetch hook and returned inside the error field. The function passed to the hook can receive the abort controller so you continue having the control to abort the request whenever needed.

@rtivital
Copy link
Member

It is a breaking change. The fix should not be implemented this way.

@rtivital rtivital closed this May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants