overleaf/services/web/frontend/stories/hooks/use-fetch-mock.tsx
Alf Eaton d91ee50762 Standardise scope/context usage in Storybook stories (#7842)
GitOrigin-RevId: 109a4357fc3b083ffbd3af5b8c98acf0f655f297
2022-05-17 08:04:12 +00:00

16 lines
364 B
TypeScript

import { useLayoutEffect } from 'react'
import fetchMock from 'fetch-mock'
fetchMock.config.fallbackToNetwork = true
/**
* Run callback to mock fetch routes, call restore() when unmounted
*/
export default function useFetchMock(callback) {
useLayoutEffect(() => {
callback(fetchMock)
return () => {
fetchMock.restore()
}
}, [callback])
}