Migrating RootPortal to functional component
This commit is contained in:
parent
09693148aa
commit
67916470fd
1 changed files with 15 additions and 30 deletions
|
@ -1,44 +1,29 @@
|
||||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
// See LICENSE.txt for license information.
|
// See LICENSE.txt for license information.
|
||||||
|
|
||||||
import PropTypes from 'prop-types'
|
import React, {useState, useEffect} from 'react'
|
||||||
import React from 'react'
|
|
||||||
import ReactDOM from 'react-dom'
|
import ReactDOM from 'react-dom'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class RootPortal extends React.PureComponent<Props> {
|
const RootPortal = React.memo((props: Props): JSX.Element => {
|
||||||
el: HTMLDivElement
|
const [el] = useState(document.createElement('div'))
|
||||||
|
|
||||||
static propTypes = {
|
|
||||||
children: PropTypes.node,
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(props: Props) {
|
|
||||||
super(props)
|
|
||||||
this.el = document.createElement('div')
|
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount(): void {
|
|
||||||
const rootPortal = document.getElementById('root-portal')
|
const rootPortal = document.getElementById('root-portal')
|
||||||
if (rootPortal) {
|
|
||||||
rootPortal.appendChild(this.el)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillUnmount(): void {
|
useEffect(() => {
|
||||||
const rootPortal = document.getElementById('root-portal')
|
|
||||||
if (rootPortal) {
|
if (rootPortal) {
|
||||||
rootPortal.removeChild(this.el)
|
rootPortal.appendChild(el)
|
||||||
|
}
|
||||||
|
return () => {
|
||||||
|
if (rootPortal) {
|
||||||
|
rootPortal.removeChild(el)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
render(): JSX.Element {
|
return ReactDOM.createPortal(props.children, el)
|
||||||
return ReactDOM.createPortal(
|
})
|
||||||
this.props.children,
|
|
||||||
this.el,
|
export default RootPortal
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue