Skip to content

Instantly share code, notes, and snippets.

@Bolza
Last active November 27, 2019 11:33
Show Gist options
  • Save Bolza/b5ab1f040b2906662d6560403aa21755 to your computer and use it in GitHub Desktop.
Save Bolza/b5ab1f040b2906662d6560403aa21755 to your computer and use it in GitHub Desktop.
class ReferenceWrapper extends Component {
static displayName = 'ReferenceWrapper'
render() {
return Children.only(this.props.children)
}
}
const renderWrapper = WrappedComponent => {
function SizeMeRenderer(props) {
const toRender = renderPlaceholder ? (
<Placeholder className={className} style={style} />
) : (
<WrappedComponent {...renderProps} {...restProps} />
)
return <ReferenceWrapper ref={explicitRef}>{toRender}</ReferenceWrapper>
}
}
// using forwardRef
const FancyButton = React.forwardRef((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
));
// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment