Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/animated/src/withAnimated.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { forwardRef, useRef, Ref, useCallback } from 'react'
import { forwardRef, useRef, Ref, useCallback, useEffect } from 'react'
import { useLayoutEffect } from 'react-layout-effect'
import {
is,
Expand Down Expand Up @@ -44,7 +44,8 @@ export const withAnimated = (Component: any, host: HostConfig) => {
const [props, deps] = getAnimatedState(givenProps, host)

const forceUpdate = useForceUpdate()
const observer = new PropsObserver(() => {

const callback = () => {
const instance = instanceRef.current
if (hasInstance && !instance) {
// Either this component was unmounted before changes could be
Expand All @@ -60,7 +61,9 @@ export const withAnimated = (Component: any, host: HostConfig) => {
if (didUpdate === false) {
forceUpdate()
}
}, deps)
}

const observer = new PropsObserver(callback, deps)

const observerRef = useRef<PropsObserver>()
useLayoutEffect(() => {
Expand All @@ -77,6 +80,7 @@ export const withAnimated = (Component: any, host: HostConfig) => {
}
})

useEffect(callback, [])
// Stop observing on unmount.
useOnce(() => () => {
const observer = observerRef.current!
Expand Down