Tillitsdone
down Scroll to discover

Custom Hooks for Framer Motion: Reusable Animations

Learn how to create reusable animation patterns in React using custom hooks with Framer Motion.

Discover practical examples and best practices for building maintainable animated applications.
thumbnail

Custom Hooks for Framer Motion: Reusability in Animated React Apps

Abstract flowing lines and geometric shapes representing motion and animation featuring holographic color spectrum with emphasis on silver and cyan tones shot from top-down perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Building engaging animations in React applications has never been more enjoyable than with Framer Motion. However, as your projects grow, you might find yourself repeating animation logic across different components. This is where custom hooks come in – they’re your secret weapon for creating reusable, maintainable animation code.

Why Custom Hooks for Animations?

Think of custom hooks as your animation recipe book. Instead of writing the same animation logic repeatedly, you can package it into a neat, reusable function. This approach not only keeps your code DRY but also makes it more maintainable and easier to update across your entire application.

Organic flowing waves and ripples in bright green and black featuring geometric patterns suggesting digital transformation captured from a 45-degree angle high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Creating Your First Animation Hook

Let’s dive into a practical example. Here’s a custom hook that handles a fade-in animation – something you might use across multiple components:

import { useAnimation } from "framer-motion";
import { useInView } from "react-intersection-observer";
import { useEffect } from "react";
export const useFadeInAnimation = () => {
const controls = useAnimation();
const [ref, inView] = useInView({
threshold: 0.2,
triggerOnce: true
});
useEffect(() => {
if (inView) {
controls.start({
opacity: 1,
y: 0,
transition: { duration: 0.5 }
});
}
}, [controls, inView]);
return {
ref,
animate: controls,
initial: { opacity: 0, y: 20 }
};
};

Real-World Applications

The beauty of custom hooks lies in their versatility. Let’s explore another powerful example – a hook for creating smooth hover animations:

export const useHoverAnimation = () => {
const [hover, setHover] = useState(false);
const animation = {
scale: hover ? 1.05 : 1,
transition: { type: "spring", stiffness: 300 }
};
return {
animation,
onHoverStart: () => setHover(true),
onHoverEnd: () => setHover(false)
};
};

Minimalist black and white composition with flowing curved lines suggesting smooth motion and transition shot from straight-on perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Best Practices for Animation Hooks

  1. Keep them focused: Each hook should handle one specific animation pattern
  2. Make them configurable: Allow customization through parameters
  3. Consider performance: Use useMemo for complex calculations
  4. Handle cleanup: Don’t forget to clean up any subscriptions or listeners

Implementation Tips

When implementing these hooks in your components, remember to keep your animation values consistent across your application. Here’s how you might use our fade-in hook:

const MyComponent = () => {
const fadeAnimation = useFadeInAnimation();
return (
<motion.div
ref={fadeAnimation.ref}
initial={fadeAnimation.initial}
animate={fadeAnimation.animate}
>
<h2>Content that fades in</h2>
</motion.div>
);
};

Conclusion

Custom hooks are your pathway to creating more maintainable and scalable animated React applications. They encapsulate animation logic in a reusable way, making your codebase cleaner and more efficient. Start small, experiment with different patterns, and gradually build up your library of animation hooks.

Dynamic abstract composition with bright neon green swirls and curves on black background suggesting energy and motion captured from low angle perspective high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.