Skip to content

Instantly share code, notes, and snippets.

@cecigarcia
Last active February 22, 2018 11:57
Show Gist options
  • Save cecigarcia/613e0e53b13b1dfc393c4e7d34292829 to your computer and use it in GitHub Desktop.
Save cecigarcia/613e0e53b13b1dfc393c4e7d34292829 to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
class CustomComponent extends Component {
apiMethod() { /* ... */ }
}
class StyledCustomComponent extends Component {
apiMethod() {
this.wrappedComponent.apiMethod();
}
render() {
return(
<CustomComponent
ref={r => this.wrappedComponent = r}
{...props}
style={{ padding: 10 }}
/>
);
}
}
class EnhancedCustomComponent extends Component {
apiMethod() {
this.wrappedComponent.apiMethod();
}
render() {
return(
<StyledCustomComponent
ref={r => this.wrappedComponent = r}
{...props}
extraInfo={info}
/>
);
}
}
class ParentComponent extends Component {
handleClick = () => this.component.apiMethod();
render() {
return (
<div>
<button onClick={this.handleClick}>call api method!</button>
<EnhancedCustomComponent ref={r => this.component = r } />
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment