Skip to content

Instantly share code, notes, and snippets.

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