Skip to content

Pragmatically set value of X #55

Answered by dbismut
sami616 asked this question in Q&A
Discussion options

You must be logged in to vote

add is just a commodity for adding vectors. All it does is essentially [ v1[0] + v2[0], v1[1] + v2[1] ]

If you only care for x, your example could be simplified like so:

function() {
  const [{ x }, set] = useSpring(() => ({ x: 0 }))
  const bind = useGesture({
    onDrag: ({ down, delta: [dx], temp = x.getValue() }) => {
      set({ x: dx + temp, immediate: down })
      return temp
    }
  })

  React.useEffect(() => {
    const snapBack = () => set({ x: 0 })
    window.addEventListener('resize', snapBack)
    return () => window.removeEventListener('resize', snapBack)
  }, [set])

  return <animated.div {...bind()} style={{ transform: x.interpolate(v => `translate3d(${v}px,0,0)`) }} />
}

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by dbismut
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #55 on December 09, 2020 10:38.