mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-25 20:32:07 +00:00
f8cb1638d1
Add useFetchMock hook for use in Storybook GitOrigin-RevId: 4eb1c5edf2f94dc6ad51358e109e29c9f62d2058
21 lines
553 B
JavaScript
21 lines
553 B
JavaScript
import { useEffect } from 'react'
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
/**
|
|
* 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)
|
|
}
|