All files / Placeholder index.js

100% Statements 13/13
100% Branches 8/8
100% Functions 4/4
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18      6x 6x 6x   6x 3x 3x 2x 3x     6x 6x    
import { useEffect, useState } from "react"
 
export default props => {
    const { children, render, ready, fallback, delayMS } = props
    const [showPlaceholder, setShowPlaceholder] = useState(false)
    const [timeoutRef, setTimeoutRef] = useState(0)
 
    useEffect(() => {
        clearTimeout(timeoutRef)
        if (ready) setShowPlaceholder(false)
        else setTimeoutRef(setTimeout(() => setShowPlaceholder(true), delayMS))
        return () => clearTimeout(timeoutRef)
    }, [ready])
 
    const rndr = children || render
    return ready ? rndr(ready) : showPlaceholder ? fallback(ready) : null
}