Skip to content

Commit

Permalink
fix: fixed nextjs/react hydration error when using portals (#1517)
Browse files Browse the repository at this point in the history
Co-authored-by: Brandon Scott <[email protected]>
  • Loading branch information
oxala and brandongregoryscott authored Sep 29, 2022
1 parent c011a50 commit c0931d2
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/portal/src/Portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ export default class Portal extends Component {
portalContainer.setAttribute('evergreen-portal-container', '')
document.body.appendChild(portalContainer)
}
}

componentDidMount() {
this.el = document.createElement('div')
portalContainer.appendChild(this.el)
this.forceUpdate()
}

componentWillUnmount() {
portalContainer.removeChild(this.el)
}

render() {
// This fixes SSR
if (!canUseDom) return null
if (!this.el) return null
return ReactDOM.createPortal(this.props.children, this.el)
}
}
Expand Down

0 comments on commit c0931d2

Please sign in to comment.