Skip to content

Instantly share code, notes, and snippets.

@avianto
Last active March 28, 2019 16:05
Show Gist options
  • Save avianto/3a230fa2c485a373d4034d5b6ab6048c to your computer and use it in GitHub Desktop.
Save avianto/3a230fa2c485a373d4034d5b6ab6048c to your computer and use it in GitHub Desktop.
Access method inside React component:
OLD (reliable but not future proof)
const ourComponent = ReactDOM.render(<OurComponent />, document.getElementById("app"));
ourComponent.someMethod() // would call the method
NEW
ReactDOM.render(<Page ref={(ourComponent) => {window.ourComponent = ourComponent}} />, document.getElementById("app"));
window.ourComponent.someMethod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment