Skip to content

Commit cf6fab9

Browse files
committed
fix: use portal to mount to body
Fixes #27.
1 parent efc6258 commit cf6fab9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
},
5555
"dependencies": {
5656
"error-stack-parser": "^1.3.6",
57-
"object-assign": "^4.0.1"
57+
"object-assign": "^4.0.1",
58+
"react-dom": "^15.1.0"
5859
}
5960
}

src/index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React, {Component, PropTypes} from 'react'
2+
import ReactDOM from 'react-dom'
23
import style from './style.js'
34
import ErrorStackParser from 'error-stack-parser'
45
import assign from 'object-assign'
56
import {isFilenameAbsolute, makeUrl, makeLinkText} from './lib'
67

7-
export default class RedBox extends Component {
8+
export class RedBoxError extends Component {
89
static propTypes = {
910
error: PropTypes.instanceOf(Error).isRequired,
1011
filename: PropTypes.string,
@@ -13,7 +14,7 @@ export default class RedBox extends Component {
1314
useColumns: PropTypes.bool,
1415
style: PropTypes.object,
1516
}
16-
static displayName = 'RedBox'
17+
static displayName = 'RedBoxError'
1718
static defaultProps = {
1819
useLines: true,
1920
useColumns: true
@@ -75,3 +76,31 @@ export default class RedBox extends Component {
7576
)
7677
}
7778
}
79+
80+
// "Portal" component for actual RedBoxError component to
81+
// render to (directly under body). Prevents bugs as in #27.
82+
export default class RedBox extends Component {
83+
static propTypes = {
84+
error: PropTypes.instanceOf(Error).isRequired
85+
}
86+
static displayName = 'RedBox'
87+
componentDidMount () {
88+
this.el = document.createElement('div')
89+
document.body.appendChild(this.el)
90+
this.renderRedBoxError()
91+
}
92+
componentDidUpdate () {
93+
this.renderRedBoxError()
94+
}
95+
componentWillUnmount () {
96+
React.unmountComponentAtNode(this.el)
97+
document.body.removeChild(this.el)
98+
this.el = null
99+
}
100+
renderRedBoxError () {
101+
ReactDOM.render(<RedBoxError {...this.props}/>, this.el)
102+
}
103+
render () {
104+
return null
105+
}
106+
}

0 commit comments

Comments
 (0)