Last active
November 1, 2017 16:58
-
-
Save eddyw/6a9ca2186d1a033e85020d59f4b18fef to your computer and use it in GitHub Desktop.
Suck Less at React: Understanding refs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import propTypes from 'prop-types' | |
import { render } from 'react-dom' | |
class Span extends React.Component { | |
static propTypes = { | |
children: propTypes.node.isRequired, | |
} | |
render() { | |
return <span>{this.props.children}</span> | |
} | |
} | |
class App extends React.Component { | |
handleRefHTMLElement(ref) { | |
console.log('span html element:', ref instanceof HTMLSpanElement) | |
} | |
handleRefInstance(ref) { | |
console.log('Span Component:', ref instanceof Span) | |
} | |
render() { | |
return [ | |
<span key="0" ref={this.handleRefHTMLElement}> | |
Span Element | |
</span>, | |
<Span key="1" ref={this.handleRefInstance}> | |
Span Component | |
</Span>, | |
] | |
} | |
} | |
render(<App />, document.getElementById("root")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment