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

Ref is not passed through a HOC #9348

Closed
PetrochukM opened this issue Apr 6, 2017 · 8 comments
Closed

Ref is not passed through a HOC #9348

PetrochukM opened this issue Apr 6, 2017 · 8 comments

Comments

@PetrochukM
Copy link

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
The parent component of a child component wrapped in a HOC cannot assign a ref attribute to the child component. The ref attribute instead refers to the HOC.

In order for the parent component to reference the child component, it must use some of the below popular workarounds:
onRef prop
kriasoft/react-starter-kit#909

innerRef prop
styled-components/styled-components#122

getInstance func
christianalfoni/formsy-react#308

What is the expected behavior?
In accordance with the decorator pattern, a child wrapped with a HOC should not be less accessible than a child not wrapped with a HOC. It is expected that the ref attribute pass through the HOC.

@PetrochukM PetrochukM changed the title HOCHOC's are not able to maintain ref prop Ref is not passed through a HOC Apr 6, 2017
@aweary
Copy link
Contributor

aweary commented Apr 10, 2017

Thanks for the request @Deepblue129, there's an existing issue at #4213 tracking this. If you have any additional input, please share it there!

@ggd543
Copy link

ggd543 commented Apr 9, 2018

you can use callback instead. here is example

class Foo extends React.Component {
  componentDidMount() {
     this.props.onRef(this);
  }

  render() {
     return 
  }
}
export default Foo;
import Foo from './foo';

class App extends React.Component {
   render() {
     <Foo onRef={foo => this.foo=foo} />
  } 
}

@arufian
Copy link

arufian commented Jun 27, 2018

@ggd543 Foo and App is not HOC ...

@Martinnord
Copy link

@ggd543 Thanks a lot!

@rollrodrig
Copy link

@ggd543 Dam dude !! this worked for me.

@gaearon
Copy link
Collaborator

gaearon commented Aug 16, 2018

You don't need this workaround now — React 16.3 and later supports ref forwarding.

https://reactjs.org/docs/forwarding-refs.html

@titulus
Copy link

titulus commented Jan 10, 2019

just want to suggest little HOC

const withRef = Component => React.forwardRef((props, ref) => <Component {...props} forwardedRef={ref} />);

@steric85
Copy link

steric85 commented Apr 7, 2020

@gaearon What should one do when the third party HOC that's being used does not support ref forwarding?

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

9 participants