Skip to content

Instantly share code, notes, and snippets.

@bvaughn
Last active March 19, 2018 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvaughn/3202a7af0aee17250c34deab0503a23c to your computer and use it in GitHub Desktop.
Save bvaughn/3202a7af0aee17250c34deab0503a23c to your computer and use it in GitHub Desktop.
Ref-forwarding examples based on an idea from Sebastian
function createHOC(SomeComponent) {
class WrapperClass extends React.Component {
render() {
return <SomeComponent {...this.props} ref={this.props.forwardedRef} />;
}
}
return Ref.forward((props, ref) => (
<WrapperClass {...props} forwardedRef={ref} />
));
}
function WrapperFunction(props) {
return <ClassComponent {...props} ref={props.forwardedRef} />;
}
// Either...
export default Ref.forward((props, ref) => (
<WrapperFunction {...props} forwardedRef={ref} />
));
// Or maybe...
export default Ref.forward((props, ref) => WrapperFunction({
...props,
forwardedRef: ref,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment