Skip to content

Commit 16bc15e

Browse files
committed
allow animated(functionComponent) pmndrs#569 pmndrs#697
1 parent bdebdd4 commit 16bc15e

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/animated/createAnimatedComponent.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import React, {
77
useImperativeHandle,
88
useRef,
99
} from 'react'
10-
import { handleRef, useForceUpdate } from '../shared/helpers'
10+
import { handleRef, useForceUpdate, is } from '../shared/helpers'
1111
import {
1212
AnimatedComponentProps,
1313
CreateAnimatedComponent,
@@ -27,12 +27,17 @@ const createAnimatedComponent: CreateAnimatedComponent = <C extends ReactType>(
2727
const attachProps = useCallback(props => {
2828
const oldPropsAnimated = propsAnimated.current
2929
const callback = () => {
30+
let didUpdate: false | undefined = false
3031
if (node.current) {
31-
const didUpdate = applyAnimatedValues.fn(
32+
didUpdate = applyAnimatedValues.fn(
3233
node.current,
3334
propsAnimated.current!.getAnimatedValue()
3435
)
35-
if (didUpdate === false) forceUpdate()
36+
}
37+
if (!node.current || didUpdate === false) {
38+
// If no referenced node has been found, or the update target didn't have a
39+
// native-responder, then forceUpdate the animation ...
40+
forceUpdate()
3641
}
3742
}
3843
propsAnimated.current = new AnimatedProps(props, callback)
@@ -56,12 +61,13 @@ const createAnimatedComponent: CreateAnimatedComponent = <C extends ReactType>(
5661
scrollLeft,
5762
...animatedProps
5863
} = propsAnimated.current!.getValue()
59-
return (
60-
<Component
61-
{...animatedProps as typeof props}
62-
ref={(childRef: C) => (node.current = handleRef(childRef, ref))}
63-
/>
64-
)
64+
65+
// Functions cannot have refs, see:
66+
// See: https://github.com/react-spring/react-spring/issues/569
67+
const refFn = is.fun(Component)
68+
? undefined
69+
: (childRef: C) => (node.current = handleRef(childRef, ref))
70+
return <Component {...animatedProps as typeof props} ref={refFn} />
6571
}
6672
)
6773
return AnimatedComponent

0 commit comments

Comments
 (0)