Last active
March 19, 2018 10:58
-
-
Save bvaughn/3202a7af0aee17250c34deab0503a23c to your computer and use it in GitHub Desktop.
Ref-forwarding examples based on an idea from Sebastian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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