diff --git a/packages/react-native/Libraries/Animated/__tests__/Animated-test.js b/packages/react-native/Libraries/Animated/__tests__/Animated-test.js index 354bd521630871..53f2af95717d10 100644 --- a/packages/react-native/Libraries/Animated/__tests__/Animated-test.js +++ b/packages/react-native/Libraries/Animated/__tests__/Animated-test.js @@ -162,6 +162,10 @@ describe('Animated tests', () => { expect(anim.__getValue()).toBe(15); }); + it('convert to JSON', () => { + expect(JSON.stringify(new Animated.Value(10))).toBe('10'); + }); + it('bypasses `setNativeProps` in test environments', async () => { const opacity = new Animated.Value(0); diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedNode.js b/packages/react-native/Libraries/Animated/nodes/AnimatedNode.js index 1835d43ec67df4..a3dd21236396e9 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedNode.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedNode.js @@ -189,4 +189,12 @@ export default class AnimatedNode { __setPlatformConfig(platformConfig: ?PlatformConfig) { this._platformConfig = platformConfig; } + + /** + * NOTE: This is intended to prevent `JSON.stringify` from throwing "cyclic + * structure" errors in React DevTools. Avoid depending on this! + */ + toJSON(): mixed { + return this.__getValue(); + } } diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 00b85b64cbe8c0..7fd38d248d6f12 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -933,6 +933,7 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A __getNativeConfig(): Object; __getPlatformConfig(): ?PlatformConfig; __setPlatformConfig(platformConfig: ?PlatformConfig): void; + toJSON(): mixed; } " `;