Skip to content

Instantly share code, notes, and snippets.

@lintonye
Created September 16, 2019 20:32
Show Gist options
  • Save lintonye/3cfea524aa0bd91d962ef22c5445d1a5 to your computer and use it in GitHub Desktop.
Save lintonye/3cfea524aa0bd91d962ef22c5445d1a5 to your computer and use it in GitHub Desktop.
function useTransform2(
mv1: MotionValue,
mv2: MotionValue,
transformer: (v1, v2) => any
) {
const mv = useMotionValue(0)
React.useEffect(() => {
const unsub1 = mv1.onChange(v1 => mv.set(transformer(v1, mv2.get())))
const unsub2 = mv2.onChange(v2 => mv.set(transformer(mv1.get(), v2)))
return () => {
unsub1()
unsub2()
}
}, [mv1, mv2, transformer, mv])
return mv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment