Skip to content

Commit

Permalink
Use ref in Mascot component rather than reaching into DOM directly (M…
Browse files Browse the repository at this point in the history
…etaMask#7893)

Accessing the dom via `document` is strongly discouraged in React.
Instead the DOM element is now referenced by ref instead.
  • Loading branch information
Gudahtt authored and yqrashawn committed Feb 10, 2020
1 parent 6128da7 commit 56a5118
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions ui/app/components/ui/mascot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import React, { createRef, Component } from 'react'
import metamaskLogo from 'metamask-logo'
import debounce from 'debounce'

Expand All @@ -22,6 +22,8 @@ export default class Mascot extends Component {
height,
})

this.mascotContainer = createRef()

this.refollowMouse = debounce(
this.logo.setFollowMouse.bind(this.logo, true),
1000
Expand Down Expand Up @@ -49,9 +51,7 @@ export default class Mascot extends Component {
}

componentDidMount () {
const targetDivId = 'metamask-mascot-container'
const container = document.getElementById(targetDivId)
container.appendChild(this.logo.container)
this.mascotContainer.current.appendChild(this.logo.container)
}

componentWillUnmount () {
Expand All @@ -66,6 +66,6 @@ export default class Mascot extends Component {
// the event emitter is on `this.props`
// and we dont get that until render
this.handleAnimationEvents()
return <div id="metamask-mascot-container" style={{ zIndex: 0 }} />
return <div ref={this.mascotContainer} style={{ zIndex: 0 }} />
}
}

0 comments on commit 56a5118

Please sign in to comment.