Skip to content

How to stop a looped animation after a complete cycle #2323

Answered by hadikman
hadikman asked this question in Support
Discussion options

You must be logged in to vote

I came up with this solution with useSprings (it probably works with useSpring too):

const SQUARES = Array.from({ length: 2 }, (_, i) => i)

const squareRef = useSpringRef()
const squareStyles = useSprings(4,
  SQUARES.map(id => ({
    ref: squareRef,
    from: { ... },
    to: { ... },
    loop: true, // must be true
  })),
)

React.useEffect(() => {
  if (isAnimate) {
    squareRef.start()
  } else {
    squareRef.start({ to: { ... }}) // important: the to object must be exactly the same as the to object in useSprings
  }
}, [squareRef, isAnimate])

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by hadikman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant