Skip to content

Commit 10b642a

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Verify that the component passed to createAnimatedComponent is not functional
Summary: Stateless functional components don't support refs and we need that for the component to work, it used to crash with this error message: `undefined is not an object (evaluating 'this._component.getScrollableNode')`. This makes it clear what the issue is. Fixes some of the errors in #10635, not sure if it fixes all the cases described in the issue though. **Test plan** Tested that passing a component with createClass or extends Component works but passing a function causes an error. [GENERAL] [ENHANCEMENT] [Animated] - Verify that the component passed to createAnimatedComponent is not functional Closes #15019 Differential Revision: D6988096 Pulled By: sahrens fbshipit-source-id: ec0ffa763245e786f44b4a1d56c0738876c25782
1 parent 7d20de4 commit 10b642a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Diff for: Libraries/Animated/src/__tests__/AnimatedNative-test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
*/
1111
'use strict';
1212

13+
const ClassComponentMock = class {};
14+
ClassComponentMock.prototype.isReactComponent = true;
15+
1316
jest
1417
.clearAllMocks()
15-
.setMock('Text', {})
16-
.setMock('View', {})
17-
.setMock('Image', {})
18+
.setMock('Text', ClassComponentMock)
19+
.setMock('View', ClassComponentMock)
20+
.setMock('Image', ClassComponentMock)
1821
.setMock('React', {Component: class {}})
1922
.setMock('NativeModules', {
2023
NativeAnimatedModule: {},

Diff for: Libraries/Animated/src/createAnimatedComponent.js

+9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ const AnimatedProps = require('./nodes/AnimatedProps');
1717
const React = require('React');
1818
const ViewStylePropTypes = require('ViewStylePropTypes');
1919

20+
const invariant = require('fbjs/lib/invariant');
21+
2022
function createAnimatedComponent(Component: any): any {
23+
invariant(
24+
typeof Component === 'string' ||
25+
(Component.prototype && Component.prototype.isReactComponent),
26+
'`createAnimatedComponent` does not support stateless functional components; ' +
27+
'use a class component instead.',
28+
);
29+
2130
class AnimatedComponent extends React.Component<Object> {
2231
_component: any;
2332
_invokeAnimatedPropsCallbackOnMount: boolean = false;

0 commit comments

Comments
 (0)