Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding warning for non-hydrated target/container refs #1841

Merged
merged 1 commit into from Dec 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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