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

do not assign ref to stateless function components #377

Closed
pwmckenna opened this issue Feb 19, 2016 · 4 comments · Fixed by #378
Closed

do not assign ref to stateless function components #377

pwmckenna opened this issue Feb 19, 2016 · 4 comments · Fixed by #378

Comments

@pwmckenna
Copy link
Contributor

On React 0.14 I get the following warning when using connectToStores on a stateless function component:

Warning: Stateless function components cannot be given refs (See ref "wrappedElement" in StatelessComponent created by StoreConnector). Attempts to access this ref will fail.

when doing something like

connectToStores(() => <div>Hello World</div>, [Store], context => ({}));

because you can't specify a ref for a stateless component.See the note at the bottom of https://facebook.github.io/react/docs/reusable-components.html#stateless-functions

Suggested Fix:

Use a check like this:
https://github.com/acdlite/recompose/blob/master/src/packages/recompose/isClassComponent.js

to conditionally pass in the ref here:
https://github.com/yahoo/fluxible/blob/master/packages/fluxible-addons-react/connectToStores.js#L56

@mridgway
Copy link
Collaborator

facebook/react#4936 for more context.

If this check is reliable, 👍 on adding it to all HoCs.

@pwmckenna
Copy link
Contributor Author

I'm happy to put together but I immediately was stumped by the choice of where to get the isComponentClass function.

  • shared utility in fluxible repo...not sure how to do this given that multiple packages need it (fluxible, fluxible-router, etc)
  • require recompose
  • pull isComponentClass into its own repo, publish, then have each package require it.
  • inline the function where needed.

Take your pick and I'll implement :)

@mridgway
Copy link
Collaborator

I looked into whether React has its own function for checking this, but it seems they just inline it: https://github.com/facebook/react/blob/bbd5a78efaf92b7ffcde2a1923112b8931855209/src/renderers/shared/reconciler/ReactCompositeComponent.js#L164 for the best performance. I think we can just use the same logic since HoC's always require a Component to be passed in anyway.

@pwmckenna
Copy link
Contributor Author

sounds good, i'll make a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants