Skip to content

Commit a6ed094

Browse files
authored
Merge pull request #3754 from motiondivision/fix-3745-popchild-ref-warning
Fix spurious React 18.3 `ref` warning in AnimatePresence non-popLayout mode
2 parents 9f251f3 + 304e7ef commit a6ed094

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎packages/framer-motion/src/components/AnimatePresence/PopChild.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,16 @@ export function PopChild({ children, isPresent, anchorX, anchorY, root, pop }: P
8787
/**
8888
* In React 19, refs are passed via props.ref instead of element.ref.
8989
* We check props.ref first (React 19) and fall back to element.ref (React 18).
90+
*
91+
* The composed ref is only used when popping the child out (pop !== false),
92+
* so we avoid reading children.props.ref otherwise. In React 18.3 that read
93+
* triggers a spurious "`ref` is not a prop" warning getter (#3745).
9094
*/
9195
const childRef =
92-
(children.props as { ref?: React.Ref<HTMLElement> })?.ref ??
93-
(children as unknown as { ref?: React.Ref<HTMLElement> })?.ref
96+
pop !== false
97+
? ((children.props as { ref?: React.Ref<HTMLElement> })?.ref ??
98+
(children as unknown as { ref?: React.Ref<HTMLElement> })?.ref)
99+
: undefined
94100
const composedRef = useComposedRefs(ref, childRef)
95101

96102
/**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { useRef } from "react"
2+
import { AnimatePresence, motion } from "../../.."
3+
import { render } from "../../../jest.setup"
4+
5+
/**
6+
* Regression test for #3745.
7+
*
8+
* In React 18.3, creating an element with a `ref` prop installs a warning
9+
* getter on `element.props.ref`. `PopChild` read `children.props.ref`
10+
* unconditionally, which fired the "`ref` is not a prop" warning even when not
11+
* in `popLayout` mode — where the composed ref is never used.
12+
*
13+
* This lives in its own test file so React's (module-level) "warned once"
14+
* flag starts fresh: other AnimatePresence tests render ref'd children and
15+
* would otherwise trip the warning before this test runs.
16+
*/
17+
describe("AnimatePresence PopChild ref access", () => {
18+
test("does not read children.props.ref in non-popLayout mode", () => {
19+
const consoleError = jest
20+
.spyOn(console, "error")
21+
.mockImplementation(() => {})
22+
23+
const Component = ({ isVisible }: { isVisible: boolean }) => {
24+
const ref = useRef<HTMLDivElement>(null)
25+
return (
26+
<AnimatePresence>
27+
{isVisible && (
28+
<motion.div key="content" ref={ref} exit={{ opacity: 0 }}>
29+
Hello
30+
</motion.div>
31+
)}
32+
</AnimatePresence>
33+
)
34+
}
35+
36+
render(<Component isVisible />)
37+
38+
const refWarning = consoleError.mock.calls.find((call) =>
39+
call.some((arg) => String(arg).includes("`ref` is not a prop"))
40+
)
41+
42+
consoleError.mockRestore()
43+
44+
expect(refWarning).toBeUndefined()
45+
})
46+
})

0 commit comments

Comments
 (0)