Skip to content

Commit

Permalink
AnimatedComponent [nfc]: Dedupe target-value expression.
Browse files Browse the repository at this point in the history
It's intentional that both of these expressions do the same thing:
the point is that when the component first renders, it should start
out in the desired state, and then when the desired state changes
and causes a rerender it'll animate to the new desired state.

So, we can make that explicit (and avoid some future change causing
an accidental discrepancy) by writing the expression just once.

Also because `visible` is already boolean, we can skip the `=== true`.
  • Loading branch information
gnprice committed Jul 28, 2021
1 parent f92460c commit c7be42f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/animation/AnimatedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ export default function AnimatedComponent(props: Props) {
style,
} = props;

const animatedValue = useRef(new Animated.Value(visible === true ? fullValue : 0));
const targetValue = visible ? fullValue : 0;

const animatedValue = useRef(new Animated.Value(targetValue));

const animate = useCallback(() => {
Animated.timing(animatedValue.current, {
toValue: visible ? fullValue : 0,
toValue: targetValue,
delay,
duration: 300,
useNativeDriver,
easing: Easing.out(Easing.poly(4)),
}).start();
}, [delay, fullValue, useNativeDriver, visible]);
}, [delay, targetValue, useNativeDriver]);

useEffect(() => {
animate();
Expand Down

0 comments on commit c7be42f

Please sign in to comment.