question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alternative to react-spring's 'useSpring' hook in Framer Motion

See original GitHub issue

Hello,

I’m trying to follow the last Joshua Comeau’s tutorial ‘Boop!’ : https://www.joshwcomeau.com/react/boop/

He used react-spring library.

For the #springs-to-the-rescue part I found a workaround :

I remove this :

const style = useSpring({
  display: 'inline-block',
  transform: isBooped ? `rotate(${rotation}deg)` : `rotate(0deg)`,
});
<animated.span onMouseEnter={trigger} style={style}>
  {children}
</animated.span>

And do this :

<motion.span
  onMouseEnter={trigger}
  style={{ display: "inline-block" }}
  animate={{
    x: isBooped ? x : 0,
    y: isBooped ? y : 0,
    rotate: isBooped ? rotation : 0,
    scale: isBooped ? scale : 1,
    transition: {
       type: "spring",
       stiffness: 300,
    }
   }}
>
    {children}
</motion.span>

But now I’m stuck in the hook part (#disconnected-boops)

I couldn’t find an alternative to the react-spring’s ‘useSpring’ hook in Framer Motion.

Is there a way to achieve this with Framer Motion?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

4reactions
deadcoder0904commented, Mar 17, 2021

Ah, I wish I checked Github. I solved it with someone’s help & was just about to post the answer.

Thank you @mattgperry for the stiffness & damping tip. I never would’ve noticed otherwise.

Codesandbox is here → https://codesandbox.io/s/react-spring-vs-framer-motion-boop-animation-did6x

And posting it here in case Codesandbox gets deleted.

useBoop.ts

import { Variants } from "framer-motion"
import React from "react"

export const useBoop = ({
	x = 0,
	y = 0,
	rotation = 0,
	scale = 1,
	timing = 150,
	transitionConfig = {
		type: "spring",
		stiffness: 300,
		damping: 10,
	},
}): [boolean, Variants, () => void] => {
	const [isBooped, setIsBooped] = React.useState(false)
	const variants = {
		boop: {
			translateX: x,
			translateY: y,
			rotate: rotation,
			scale: scale,
			transition: transitionConfig,
		},
		rest: {
			rotate: 0,
			scale: 1,
		},
	}
	React.useEffect(() => {
		if (!isBooped) return

		const timeoutId = window.setTimeout(() => {
			setIsBooped(false)
		}, timing)

		return () => {
			window.clearTimeout(timeoutId)
		}
	}, [isBooped, timing])

	const trigger = React.useCallback(() => {
		setIsBooped(true)
	}, [])

	return [isBooped, variants, trigger]
}

App.tsx

import React from "react"
import { motion } from "framer-motion"

import { useBoop } from "./useBoop"
import { Cog } from "../Cog"

export const Motion = () => {
	const [isBooped, variants, trigger] = useBoop({
		rotation: 20,
		timing: 150,
	})

	return (
		<>
			<h1 className="text-6xl text-pink-500">Framer Motion</h1>
			<motion.span
				onMouseEnter={trigger}
				animate={isBooped ? "boop" : "rest"}
				variants={variants}
			>
				<Cog className="w-16 h-16" />
			</motion.span>
		</>
	)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a `useSpring()` alternative for framer-motion?
The problem is, that the bottom sheet is powered by react-spring and @use-gestures but i want to use framer-motion for the animation part....
Read more >
Best React animation library: Top 7 libraries compared
We will cover the following animation libraries for React: React Spring; Framer Motion; React Transition Group; React Motion; React Move ...
Read more >
Top React Animation Library | React Spring vs. Framer motion
Code Repo: https://github.com/ipenywis/best- react -animation-libraries Github: https://github.com/ipenywis Follow me on Twitter: ...
Read more >
React-Spring vs Framer Motion: Comparing Examples in Two ...
Framer Motion and react-spring both solve unique animation challenges and execute with high performance. Framer Motion uses the more ...
Read more >
Top 7 React Animation Libraries in 2022 | Syncfusion Blogs
Framer Motion is a popular React animation toolkit designed to make ... import { useSpring, animated } from '@react-spring/web'; export ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found