Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify that the component passed to createAnimatedComponent is not functional #15019

9 changes: 6 additions & 3 deletions Libraries/Animated/src/__tests__/AnimatedNative-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
*/
'use strict';

const ClassComponentMock = class {};
ClassComponentMock.prototype.isReactComponent = true;

jest
.clearAllMocks()
.setMock('Text', {})
.setMock('View', {})
.setMock('Image', {})
.setMock('Text', ClassComponentMock)
.setMock('View', ClassComponentMock)
.setMock('Image', ClassComponentMock)
.setMock('React', {Component: class {}})
.setMock('NativeModules', {
NativeAnimatedModule: {},
Expand Down
9 changes: 9 additions & 0 deletions Libraries/Animated/src/createAnimatedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ const AnimatedProps = require('./nodes/AnimatedProps');
const React = require('React');
const ViewStylePropTypes = require('ViewStylePropTypes');

const invariant = require('fbjs/lib/invariant');

function createAnimatedComponent(Component: any): any {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Flow checking within the RN codebase, you could add something like this:

createAnimatedComponent<P>(
  Component: Class<React.Component<P>>
): Class<React.Component<*>> {
  ...
}

I believe this will filter out functions too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember trying to type this properly but I hit some issue. React components typing changed in flow 0.53 I can try again to see if I can figure out something that works.

invariant(
typeof Component === 'string' ||
(Component.prototype && Component.prototype.isReactComponent),
'`createAnimatedComponent` does not support stateless functional components; ' +
'use a class component instead.',
);

class AnimatedComponent extends React.Component<Object> {
_component: any;
_invokeAnimatedPropsCallbackOnMount: boolean = false;
Expand Down