本文整理汇总了TypeScript中react.useLayoutEffect函数的典型用法代码示例。如果您正苦于以下问题:TypeScript useLayoutEffect函数的具体用法?TypeScript useLayoutEffect怎么用?TypeScript useLayoutEffect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了useLayoutEffect函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: useComponentSize
export function useComponentSize(ref: React.RefObject<HTMLElement>) {
let [componentSize, setComponentSize] = useState(getSize(ref.current));
const handleResize = useCallback(
function handleResize() {
if (ref.current) {
setComponentSize(getSize(ref.current));
}
},
[ref]
);
useLayoutEffect(() => {
if (!ref.current) {
return;
}
if (SUPPORTS_RESIZE_OBSERVER) {
let resizeObserver = new ResizeObserver(() => handleResize());
resizeObserver.observe(ref.current);
return () => {
resizeObserver.disconnect();
};
} else {
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('resize', handleResize);
};
}
}, [handleResize, ref]);
return componentSize;
}
示例2: useUnmount
export function useUnmount() {
const unmounted = useRef(true)
useLayoutEffect(() => {
unmounted.current = false
return () => {
unmounted.current = true
}
}, [])
return unmounted
}
示例3: useOnScreen
export function useOnScreen(ref: React.RefObject<Element>, options?: IntersectionObserverInit) {
const [isIntersecting, setIntersecting] = useState(false);
useLayoutEffect(() => {
if (ref.current) {
const observer = new IntersectionObserver(([entry]) => setIntersecting(entry.isIntersecting), options);
observer.observe(ref.current);
return () => {
observer.disconnect();
};
}
}, [ref, options]);
return isIntersecting;
}
示例4: useResizeObserver
export function useResizeObserver(target: React.RefObject<HTMLElement>) {
const [rect, setRect] = useState(getZeroRect);
const observer: React.MutableRefObject<ResizeObserver | null> = useRef(null);
useLayoutEffect(() => {
function handleResize(entries: Array<ResizeObserverEntry>) {
setRect(entries[0].contentRect);
}
observer.current = new ResizeObserver(handleResize);
if (target.current !== null) {
observer.current.observe(target.current);
}
return () => {
if (observer.current !== null) {
observer.current.disconnect();
observer.current = null;
}
};
}, []);
return rect;
}
示例5: usePlayer
export function usePlayer(
ref: React.RefObject<HTMLCanvasElement>,
frames: Frame[]
) {
const timerRef = useRef(0)
const [index, setIndex] = useState(0)
const [paused, setPaused] = useState(false)
function draw(image: HTMLImageElement) {
if (!image || !ref.current) return
const canvas = ref.current
if (canvas.width !== image.width) {
canvas.width = image.width
}
if (canvas.height !== image.height) {
canvas.height = image.height
}
const ctx = canvas.getContext('2d')!
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(image, 0, 0)
}
function next() {
const lastFrame = frames.length - 1
if (index === lastFrame) {
setIndex(0)
} else {
setIndex(index + 1)
}
}
function toggle() {
if (paused) {
setPaused(false)
next()
} else {
window.clearTimeout(timerRef.current)
setPaused(true)
}
}
function rewind() {
window.clearTimeout(timerRef.current)
setPaused(true)
setIndex(0)
}
useLayoutEffect(() => {
const { delay, image } = frames[index]
draw(image)
if (!paused) {
timerRef.current = window.setTimeout(next, delay)
}
return () => {
window.clearTimeout(timerRef.current)
}
}, [index])
return { index, paused, toggle, rewind }
}