import React, { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'; export function throttle(callback, limit) { let handler = null; // Initially, we're not waiting return (...args) => { // We return a throttled function if (!handler) { // If we're not waiting callback(...args); // Execute users function handler = setTimeout(() => handler = null, limit); } } } function debounce(callback, delay) { let handler = null; return (...args) => { clearTimeout(handler); handler = setTimeout(() => callback(...args), delay); } } const transTime = 200 const elastic = `transform ${transTime}ms cubic-bezier(0.4, 0.0, 0.2, 1)`; export const Carousel = () => { const arr = [1, 2, 3] const ref = useRef(null) const [dragging, setDragging] = useState(false) // const dragging = useRef(false) const [rect, setRect] = useState({}) const [pg, setPg] = useState(0) const [dragLeft, setDragLeft] = useState(0) useEffect(() => { setRect(ref.current.getBoundingClientRect()) }, [ref]) const updateSize = useCallback( debounce( // throttle( () => { setRect(ref.current.getBoundingClientRect()) } , 200 ) , [] ) // const updateSize = () => setRect(ref.current.getBoundingClientRect()) useEffect(() => { // dragging.current = false // console.log(dragging) // setDragLeft(pg * rect.width) // setDragging(false) }, [rect]) useEffect(() => { window.addEventListener('resize', updateSize) return () => window.removeEventListener('resize', updateSize) }, []) return <>