So I try to have an overlay over my gatsby site for about a split second to let it load the correct breakpoint in the back. How can I achieve this?
So I set an Interval and some State to load the Overlay and make it disappear after an interval. But the problem after each page reload the overlay appears again. I have no state management like redux and I need the Overlay to only appear after every refresh or entrance from external links but not clicking on internal links.
I've tried useEffect
with only render on the mount but this does not do the trick as internal link changes will trigger a remount
I figured out that I could use the Browser API onInitialClientRender. But I don't understand how a value generated in there can be accessed outside of the gatsby-browser.js. In general, I have issues understanding how to use these APIs.
customHooks.js
export function useInterval(callback, delay) {
const savedCallback = useRef()
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback
}, [callback])
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current()
}
if (delay !== null) {
let id = setInterval(tick, delay)
return () => clearInterval(id)
}
}, [delay])
}
Layout.js
let [loadingIndicator, setLoadingIndicator] = useState(true)
const delay = 500
useInterval(() => {
setLoadingIndicator(false)
}, delay)
So the expected result would be a way to tell on the initial client render to set a state which tells the Overlay to be rendered and after this one render the state should change to disable the render of the overlay.