Skip to content

Commit

Permalink
Adding warning for non-hydrated refs (#1841)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry committed Dec 21, 2022
1 parent f58fea8 commit 90d4f4f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/framer-motion/src/value/use-scroll.ts
Expand Up @@ -4,13 +4,21 @@ import { motionValue } from "."
import { useConstant } from "../utils/use-constant"
import { useEffect } from "react"
import { useIsomorphicLayoutEffect } from "../three-entry"
import { warning } from "hey-listen"

interface UseScrollOptions extends Omit<ScrollOptions, "container" | "target"> {
container?: RefObject<HTMLElement>
target?: RefObject<HTMLElement>
layoutEffect?: boolean
}

function refWarning(name: string, ref?: RefObject<HTMLElement>) {
warning(
Boolean(!ref || ref.current),
`You have defined a ${name} options but the provided ref is not yet hydrated, probably because it's defined higher up the tree. Try calling useScroll() in the same component as the ref, or setting its \`layoutEffect: false\` option.`
)
}

const createScrollMotionValues = () => ({
scrollX: motionValue(0),
scrollY: motionValue(0),
Expand All @@ -31,6 +39,9 @@ export function useScroll({
: useEffect

useLifecycleEffect(() => {
refWarning("target", target)
refWarning("container", container)

return scroll(
({ x, y }) => {
values.scrollX.set(x.current)
Expand Down

0 comments on commit 90d4f4f

Please sign in to comment.