Skip to content

Instantly share code, notes, and snippets.

@jimfb
Last active November 6, 2018 04:41
Show Gist options
  • Save jimfb/32b587ee6177665fb4cf to your computer and use it in GitHub Desktop.
Save jimfb/32b587ee6177665fb4cf to your computer and use it in GitHub Desktop.
class MyWrapper {
return React.Children.only(this.props.children);
}
class MyLibraryComponent {
render() {
return <div><span><whatever><MyWrapper ref=...>{this.props.statelessComponentThatIWantToReference}</MyWrapper></whatever></span></div>;
}
}
// ReactDOM.findDOMNode(mywrapperref) effectively returns a reference to the stateless component.
// You can find the DOM node of the stateless component, etc.
// It no longer matters if your users are using stateless components or real components.
@ctrlplusb
Copy link

Thanks for this. It helped me sort out my HOC nicely!

@kohlmannj
Copy link

@ctrlplusb @jimfb Is there a more comprehensive example of this design pattern somewhere? I'm having a hard time implementing this.

@kohlmannj
Copy link

For posterity: @ctrlplusb's react-sizeme is a good example of this design pattern: https://github.com/ctrlplusb/react-sizeme/blob/master/src/sizeMe.js (thanks Sean!)

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