Search code examples
javascriptreactjsframer-motionpose

Apply motion to react component Framer-Motion


I know that I can apply motion directly to element/HTMLtag like this:

<motion.div>some content</div>

But how can I apply it to this?

<Comp />

Without wrapping it inside another HTML element, like in React-Transition-Group library.

Framer API provides Frame component, but it acts like permanent additional HTML element with own styling, and it is messing my layout.


Solution

  • If anyone comes to this page seeking for the solution of how to apply motion from Framer-Motion library to your React Component and not the direct DOM element like "div" or "span", here it is:

    motion.custom()
    

    Example of use:

    import { Link } from "react-router-dom"
    
    const MotionLink = motion.custom(Link)
    
    return <MotionLink />
    

    As for today it is not mentioned in the official documentation, or it is in someplace deep and hard to find.

    I had found it in BUG reports here, there is a Codesanbox that illustrates my example, created by the person who reported a bug.

    Version 4.1.3 or higher

    Edit: As mentioned by @Aleksandr K. below, use the following for Version >=4.1.3:

    const MotionLink = motion(Link)