-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Verify that the component passed to createAnimatedComponent is not functional #15019
Conversation
@@ -1732,6 +1732,12 @@ class AnimatedProps extends Animated { | |||
} | |||
|
|||
function createAnimatedComponent(Component: any): any { | |||
invariant( | |||
Component.prototype.render, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to check this? It does work but meh...
5f55c4a
to
704fbd5
Compare
@sahrens Could you have a look at this? |
@facebook-github-bot label Needs more information @facebook-github-bot label Core Team Generated by 🚫 dangerJS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, is this the best way to check? I thought React had some helper functions that would be more robust.
@@ -17,7 +17,15 @@ const AnimatedProps = require('./nodes/AnimatedProps'); | |||
const React = require('React'); | |||
const ViewStylePropTypes = require('ViewStylePropTypes'); | |||
|
|||
const invariant = require('fbjs/lib/invariant'); | |||
|
|||
function createAnimatedComponent(Component: any): any { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@sahrens Yea I’m not sure if there is a better way, I commented on the diff to ask that but it doesn’t show up since I rebased. Maybe ask someone from the react team. |
@bvaughn Is there a public API to check if a component is functional? (needed to know if refs are available). |
FWIW we use this type of duck typing in React too (eg
|
@janicduplessis I tried to find reviewers for this pull request and wanted to ping them to take another look. However, based on the blame information for the files in this pull request I couldn't find any reviewers. This sometimes happens when the files in the pull request are new or don't exist on master anymore. Is this pull request still relevant? If yes could you please rebase? In case you know who has context on this code feel free to mention them in a comment (one person is fine). Thanks for reading and hope you will continue contributing to the project. |
704fbd5
to
affbd87
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, sorry for not seeing your earlier update. This looks good to me.
function createAnimatedComponent(Component: any): any { | ||
invariant( | ||
Component.prototype && Component.prototype.isReactComponent, | ||
'`createAnimatedComponent` does not support stateless functional components, ' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny nit: "," should be a ";" I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll trust you on that one. I never know when ;
should be used, I guess I wasn't listening at school that day :)
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions. |
@janicduplessis would you want to rebase this? |
@ide Looks like tests need to be updated to pass this new check, will try to find some time to update this soon. |
Updated and re-tested, we actually need to allow string components too since in prod views are just RCTView strings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sahrens is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
…nctional 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 facebook#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 facebook#15019 Differential Revision: D6988096 Pulled By: sahrens fbshipit-source-id: ec0ffa763245e786f44b4a1d56c0738876c25782
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.
Release Notes
[GENERAL] [ENHANCEMENT] [Animated] - Verify that the component passed to createAnimatedComponent is not functional