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

WIP Refactor useMotionRef for Improved Lifecycle Mgmt #2283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
46 changes: 39 additions & 7 deletions packages/framer-motion/src/motion/utils/use-motion-ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react"
import { useCallback } from "react"
import { useCallback, useEffect } from "react"
import type { VisualElement } from "../../render/VisualElement"
import { isRefObject } from "../../utils/is-ref-object"
import { VisualState } from "./use-visual-state"
Expand All @@ -13,14 +13,46 @@ export function useMotionRef<Instance, RenderState>(
visualElement?: VisualElement<Instance> | null,
externalRef?: React.Ref<Instance>
): React.Ref<Instance> {
/**
* This React effect ensures the systematic unmounting of 'visualElement'
* during the component's unmount phase. This cleanup is especially pivotal
* in contexts where the component's rendering might have been affected by asynchronous
* operations, such as with React.lazy and Suspense.
*
* Given that 'visualElement' animations are allowed to continue even when certain
* child components might be rendered invalid by promises from React.lazy, it becomes
* paramount to ensure that resources or side-effects associated with this DOM node
* are properly managed and cleaned up to avoid potential pitfalls.
*/
useEffect(() => {
return () => {
if (visualElement) {
visualElement.unmount()
}
}
}, [visualElement])

return useCallback(
(instance: Instance) => {
instance && visualState.mount && visualState.mount(instance)

if (visualElement) {
instance
? visualElement.mount(instance)
: visualElement.unmount()
/**
* This section manages the lifecycle of 'visualState' and 'visualElement' based on
* the presence of the 'instance' variable, which corresponds to a real DOM node.
*
* - When a valid DOM node (represented by 'instance') is detected, both 'visualState'
* and 'visualElement' are triggered to mount. This signifies the preparation or
* setup of visual components or states based on the detected node.
*
* - A complex scenario emerges when 'instance' becomes null, particularly within
* the environment of an outer Suspense boundary. With React.lazy, components are
* loaded lazily and the promises (or thenables) might render certain child components
* invalid based on their resolution or rejection. This can lead to situations where
* the expected DOM node isn't available. Yet, in these cases, the 'visualElement'
* doesn't get immediately unmounted. Animations tied to it persist, maintaining a
* consistent visual experience.
*/
if (instance) {
visualState.mount && visualState.mount(instance)
visualElement && visualElement.mount(instance)
}

if (externalRef) {
Expand Down