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

Connected components may not be stateless #99

Open
babakness opened this issue Jan 13, 2018 · 11 comments
Open

Connected components may not be stateless #99

babakness opened this issue Jan 13, 2018 · 11 comments

Comments

@babakness
Copy link

reduxjs/react-redux#141

The component returned by connect doesn't allow for refs ie <Component ref='foobar' />. React gives warning "Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail."

@babakness
Copy link
Author

babakness commented Jan 14, 2018

For reference my current workaround is to connect explicitly using const boundActions = bindActions(actions,store) then to do something like the following

<Foo ref="bar" {...boundActions} {...store.getState()} />

Along with store.subscribe( () => this.forceUpdate() )

@matheusml
Copy link
Member

Hi @babakness, thanks for this.

Do you have a solution in mind?

@jetako
Copy link
Contributor

jetako commented Jan 17, 2018

I think this is partially resolved in master via #98.

connect was changed to return a regular Component instead of a function. However, ref might point to the outer HOC, not the wrapped component as you may want.

@matheusml
Copy link
Member

Maybe the solution to this is to pass down the ref prop to the component that's being HOC'd.

What you think @babakness, @jetako?

@jetako
Copy link
Contributor

jetako commented Jan 18, 2018

@matheusml I would consider following the approach taken by react-redux. connect accepts an opt-in withRef option. When true, ref points to the wrapped component.

@matheusml
Copy link
Member

@jetako ok, that's a valid option.

Could you please open a PR with this?

@jetako
Copy link
Contributor

jetako commented Jan 20, 2018

@matheusml Afraid I won't have the bandwidth for this any time soon. @babakness ?

@brennanerbz
Copy link

export default function connect(mapToProps, actions = {}) {
  return Child => {
    return React.forwardRef((props, ref) => {
      return (
        <Connect {...props} mapToProps={mapToProps} actions={actions}>
          {mappedProps => <Child ref={ref} {...mappedProps} {...props} />}
        </Connect>
      );
    });
  };
}

@leobetosouza
Copy link
Contributor

leobetosouza commented Aug 15, 2018

I need this feature... I will work on it after #119

@leobetosouza
Copy link
Contributor

To imprementing the solution using React.forwardRef we need to upgrade React to at least React 16.3

And, as recommended is React Docs, increase a major version of redux-zero.

This is OK, @matheusml?

@matheusml
Copy link
Member

Yes it is, @leobetosouza

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

No branches or pull requests

5 participants