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

Connect's wrapped component can't be a stateless function #141

Closed
dariocravero opened this issue Oct 8, 2015 · 9 comments · Fixed by CrocoDillon/crocodillon.com#22, Dean177/lunch#8 or lyleg/dartbuddy#2

Comments

@dariocravero
Copy link
Contributor

If the component wrapped with connect is a stateless function component, React warns about the internal ref that the wrapper holds with the following message Warning: Stateless function components cannot be given refs (See ref "wrappedInstance" in App created by Connect(App)). Attempts to access this ref will fail.

import { connect } from 'react-redux';
import React from 'react';

export default connect(mapStateToProps)(props => <div />);

Would it be useful to check whether the component is stateless or not to decide when to apply the ref or do we always need it?

@troch
Copy link

troch commented Oct 8, 2015

You can suppress that warning by using troch/react-stateless as a workaround for now.

What is the wrappedInstance ref used for?

@gaearon
Copy link
Contributor

gaearon commented Oct 9, 2015

People use it for tests and side effects (i.e. calling instance methods) I presume.
Personally I don't have use for it, but it was repeatedly asked.

@hex13
Copy link

hex13 commented Oct 11, 2015

So is it okay to use stateless functions with React-Redux and I can ignore this warning? (I do not use ref or wrappedInstance).

@gaearon
Copy link
Contributor

gaearon commented Oct 11, 2015

Yes, you can ignore the warning for now.

@hex13
Copy link

hex13 commented Oct 11, 2015

Ok, thx :)

@gaearon
Copy link
Contributor

gaearon commented Oct 15, 2015

This is fixed via 2d3d0be.
We're making withRef an opt-in parameter, with false by default.

@dariocravero
Copy link
Contributor Author

Thanks! :)

ericf added a commit to formatjs/formatjs that referenced this issue Oct 22, 2015
React 0.14 stateless components can't have refs, so this makes adding
a `ref` to the wrapped component optional and **off by default**.

This follows react-redux's implementation of `connect()`.

See: reduxjs/react-redux#141
@mini-eggs
Copy link

I ran into something that may be related.

While using connect with a stateless component that had a child component with state (extends React.Component): the child component's componentWillReceiveProps would never fire on prop change. Instead the child component with state would rerender and it's componentDidMount would fire again. I ran into this within a create-react-native-app project.

If this is indeed an issue I would be happy to look into it and create a PR, I could also start a branch on the project to easily reproduce this (I've since removed the stateless component with connect).

@markerikson
Copy link
Contributor

@mini-eggs : hmm. If it's something you can reproduce, please go ahead and file a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment