Skip to content

Commit fe53cae

Browse files
rozelefacebook-github-bot
authored andcommitted
Deduct offset from getValue result when detaching
Summary: The NativeAnimated `getValue` API returns `value + offset`: Android: iOS: https://github.com/facebook/react-native/blob/main/Libraries/NativeAnimation/Nodes/RCTValueAnimatedNode.m#L44 Android: https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/animated/ValueAnimatedNode.java#L36 When we store the value after detaching the NativeAnimated node, it stores the result of the NativeAnimated `getValue` call to the `_value` property, so if we call `__getValue` at some later point on the `AnimatedValue`, we will count the offset twice. This change deducts the offset value from the result returned from `getValue` when storing the latest value. Changelog: [General][Fixed] - AnimatedValue.__detach should store getValue result with offset deducted Reviewed By: yungsters Differential Revision: D32987003 fbshipit-source-id: 488d1fe512f886c7a9de1e5a4de8f19441ebd81e
1 parent c034b7e commit fe53cae

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Libraries/Animated/__tests__/AnimatedNative-test.js

+22
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ describe('Native Animated', () => {
134134
expect(opacity.__getValue()).toBe(1);
135135
});
136136

137+
it('should deduct offset when saving value on unmount', () => {
138+
NativeAnimatedModule.getValue = jest.fn((tag, saveCallback) => {
139+
// Assume current raw value of value node is 0.5, the NativeAnimated
140+
// getValue API returns the sum of raw value and offset, so return 1.
141+
saveCallback(1);
142+
});
143+
const opacity = new Animated.Value(0);
144+
opacity.setOffset(0.5);
145+
opacity.__makeNative();
146+
147+
const root = TestRenderer.create(<Animated.View style={{opacity}} />);
148+
const tag = opacity.__getNativeTag();
149+
150+
root.unmount();
151+
152+
expect(NativeAnimatedModule.getValue).toBeCalledWith(
153+
tag,
154+
expect.any(Function),
155+
);
156+
expect(opacity.__getValue()).toBe(1);
157+
});
158+
137159
it('should extract offset', () => {
138160
const opacity = new Animated.Value(0);
139161
opacity.__makeNative();

Libraries/Animated/nodes/AnimatedValue.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class AnimatedValue extends AnimatedWithChildren {
100100
__detach() {
101101
if (this.__isNative) {
102102
NativeAnimatedAPI.getValue(this.__getNativeTag(), value => {
103-
this._value = value;
103+
this._value = value - this._offset;
104104
});
105105
}
106106
this.stopAnimation();

0 commit comments

Comments
 (0)