1
1
mirror of https://github.com/c8r/x0.git synced 2024-09-11 13:45:52 +03:00

Add Catch component back

This commit is contained in:
Brent Jackson 2018-05-22 18:13:25 -04:00
parent a1e9d9aaae
commit c9498aa0bd

View File

@ -53,6 +53,45 @@ const DefaultApp = ({ render, routes }) => (
</Switch>
)
class Catch extends React.Component {
static getDerivedStateFromProps (props, state) {
if (!state.err) return null
return { err: null }
}
state = {
err: null
}
componentDidCatch (err) {
this.setState({ err })
}
render () {
const { err } = this.state
if (err) {
return (
<pre
children={err.toString()}
style={{
color: 'white',
backgroundColor: 'red',
fontFamily: 'Menlo, monospace',
fontSize: '14px',
margin: 0,
padding: '16px',
minHeight: '128px',
whiteSpace: 'prewrap'
}}
/>
)
}
return this.props.children
}
}
const Router = IS_CLIENT ? BrowserRouter : StaticRouter
const App = withRouter(APP ? (require(APP).default || require(APP)) : DefaultApp)
@ -96,22 +135,27 @@ export default class Root extends React.Component {
basename={basename}
location={path}>
<React.Fragment>
<App
routes={routes}
render={appProps => (
routes.map(({ Component, ...route }) => (
<Route
{...route}
render={props => (
<Component
{...props}
{...appProps}
{...route.props}
/>
)}
/>
))
)} />
<Catch>
<App
routes={routes}
render={appProps => (
routes.map(({ Component, ...route }) => (
<Route
{...route}
render={props => (
<Catch>
<Component
{...props}
{...appProps}
{...route.props}
/>
</Catch>
)}
/>
))
)}
/>
</Catch>
</React.Fragment>
</Router>
)