Merge branch 'fix-typo-drive' into 'main'
[ProtonMail-WebClient.git] / packages / hooks / useAsyncError.ts
blobd223df5573cb0f344c920d291a27a94b0cad1a8d
1 import { useEffect, useState } from 'react';
3 /**
4  * Custom hook that allows throwing an error asynchronously.
5  * Source: https://medium.com/trabe/catching-asynchronous-errors-in-react-using-error-boundaries-5e8a5fd7b971
6  * @returns {function} throwError - A function that can be called to throw an error.
7  */
8 const useAsyncError = () => {
9     const [error, setError] = useState<Error | null>(null);
11     useEffect(() => {
12         if (error) {
13             throw error;
14         }
15     }, [error]);
17     return setError;
20 export default useAsyncError;