Skip to content

Commit

Permalink
Animated: Restore AnimatedNode.prototype.toJSON (#46498)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46498

Looks like this is still necessary because we still run into this error when using the Components tab when using React DevTools:

> TypeError: cyclical structure in JSON object

This effectively reverts #46382.

Changelog:
[General][Changed] - AnimatedNode (and its subclasses) once again implement `toJSON()`.

Reviewed By: javache

Differential Revision: D62690380

fbshipit-source-id: d5b7c1d156b49838abefe48a7d7b61471cc3488a
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 15, 2024
1 parent 7d621f5 commit 7bd4a54
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
"
`;
Expand Down

0 comments on commit 7bd4a54

Please sign in to comment.