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

createAnimatedComponent should support stateless functional components [React Hooks] #22066

Closed
xzilja opened this issue Nov 1, 2018 · 10 comments
Labels
API: Animated Resolution: Locked This issue was locked by the bot. Type: Discussion Long running discussion.

Comments

@xzilja
Copy link
Contributor

xzilja commented Nov 1, 2018

For Discussion

I am starting to play with hooks in react native following guide posted by hramos
#21967 (comment)

Off the bat I got an error

createAnimatedComponent does not support stateless functional components; use a class instead.

This was as a result of me converting my simplest components (that don't rely on anything related to hooks) to functions and I assume will be an issue further down the road since vision of react for the future is to use less classes and more function components.

@hramos
Copy link
Contributor

hramos commented Nov 13, 2018

I don't think this requires a new issue, you can comment on the original issue itself.

@hramos hramos closed this as completed Nov 13, 2018
@MrLoh
Copy link

MrLoh commented Apr 4, 2019

@hramos this is still an issue in 0.59.1, can this be reopened?

@adkenyon
Copy link
Contributor

adkenyon commented Apr 9, 2019

@MrLoh It looks as though createAnimatedComponent requires a ref (which functional components don't provide). You can get around the issue by using React.forwardRef with your function component.

@MrLoh
Copy link

MrLoh commented Apr 10, 2019

yeah, that was an obvious solution. Should have thought of that myself

@shubhamdeol
Copy link

@MrLoh Can you please explain how to did this, using forwardRef ?

@MrLoh
Copy link

MrLoh commented Jul 19, 2019

@shubhamdeol

const MyFunctionalComponent = React.forwardRef((props, ref) => <View ref={ref}> ... );

then you can do

createAnimatedComponent(MyFunctionalComponent);

@shubhamdeol
Copy link

Actually. The component which is stateless is inside library. Can I pass ref to that without forking the library code?

@MrLoh
Copy link

MrLoh commented Jul 19, 2019

Does the library component accept a ref prop? Not sure wether things work if Animated doesn't have a ref, you might have to ask the library maintainers to add a ref prop.

@shubhamdeol
Copy link

Okay, Thanks
I will do this.

@tberman
Copy link

tberman commented Aug 14, 2019

I just tried this w/ a forwardRef and received the same error, and looking at the code of how forwardRef works, it is unclear it would pass this check. Does the forwardRef trick still work for people running on 16.8?

@facebook facebook locked as resolved and limited conversation to collaborators Nov 13, 2019
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Nov 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API: Animated Resolution: Locked This issue was locked by the bot. Type: Discussion Long running discussion.
Projects
None yet
Development

No branches or pull requests

7 participants