overleaf/services/web/frontend/stories/hooks/use-fetch-mock.js
Alf Eaton ae76850d39 Set fetchMock.config.fallbackToNetwork for Storybook (#7369)
GitOrigin-RevId: df957893c8495c582d721716daa20030deb02f72
2022-04-05 12:20:47 +00:00

22 lines
595 B
JavaScript

import { useEffect } 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) {
useEffect(() => {
return () => {
fetchMock.restore()
}
}, [])
// Running fetchMock.restore() here as well,
// in case there was an error before the component was unmounted.
fetchMock.restore()
// The callback has to be run here, rather than in useEffect,
// so it's run before the component is rendered.
callback(fetchMock)
}