'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 1:[1]

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.

Solution 2:[2]

Without using any internal fuctions, You just need to wrap it with any motion element:

<motion.div>
  <Comp />
</motion.div>

You can notice such behavior across examples in the docs, like of Side Menu example.

Solution 3:[3]

motion.custom was deprecated as of v4.0 in favour of motion(Component) or motion("Component").

Your code would simply look like this

const MotionComp = motion(Comp)

return <MotionComp>some content</MotionComp>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Jay Wick