Skip to content

Instantly share code, notes, and snippets.

@panchaow
Created April 20, 2018 14:16
Show Gist options
  • Save panchaow/3552a3f93710deb21622dbafb245607d to your computer and use it in GitHub Desktop.
Save panchaow/3552a3f93710deb21622dbafb245607d to your computer and use it in GitHub Desktop.
new Refs API in React
class MyComponent extends React.Component {
constructor(props) {
super(props);
// Refs are created using React.createRef().
// Refs are commonly assigned to an instance property when a component is constructed so they can be referened throughout the component.
this.myRef = React.createRef();
}
render() {
// Refs are attached to React elements via the ref attribute
return <div ref={this.myRef} />;
}
}
// assessing refs: const node = this.myRef.current;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment