forked from buttons/react-github-btn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (40 loc) · 1.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import React, { PureComponent } from 'react'
class GitHubButton extends PureComponent {
constructor (props) {
super(props)
this.$ = React.createRef()
this._ = React.createRef()
}
render () {
return React.createElement('span', { ref: this.$ }, React.createElement('a', { ...this.props, ref: this._ }, this.props.children))
}
componentDidMount () {
this.paint()
}
getSnapshotBeforeUpdate () {
this.reset()
return null
}
componentDidUpdate () {
this.paint()
}
componentWillUnmount () {
this.reset()
}
paint () {
const _ = this.$.current.appendChild(document.createElement('span'))
import(/* webpackMode: "eager" */ 'github-buttons').then(({ render }) => {
if (this._.current != null) {
render(_.appendChild(this._.current), function (el) {
try {
_.parentNode.replaceChild(el, _)
} catch (_) {}
})
}
})
}
reset () {
this.$.current.replaceChild(this._.current, this.$.current.lastChild)
}
}
export default GitHubButton