2020-03-13 20:05:45 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-01-28 14:24:01 +00:00
|
|
|
import { useState, useCallback } from '@wordpress/element';
|
2020-03-13 20:05:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for throwing an error in a React Hook.
|
|
|
|
*
|
|
|
|
* @see https://github.com/facebook/react/issues/14981
|
|
|
|
*/
|
2021-03-10 15:03:26 +00:00
|
|
|
export const useThrowError = (): ( ( error: Error ) => void ) => {
|
2020-03-13 20:05:45 +00:00
|
|
|
const [ , setState ] = useState();
|
2021-03-10 15:03:26 +00:00
|
|
|
return useCallback( ( error: Error ): void => {
|
|
|
|
setState( () => {
|
|
|
|
throw error;
|
|
|
|
} );
|
|
|
|
}, [] );
|
2020-03-13 20:05:45 +00:00
|
|
|
};
|