-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ConfigProvider): add ErrorBoundary
- Loading branch information
Showing
8 changed files
with
280 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# ErrorBoundary 捕获错误 | ||
|
||
- order: 5 | ||
|
||
使用 `<ErrorBoundary>` 可以避免由于局部区域的错误,所引起的页面白屏。 | ||
|
||
:::lang=en-us | ||
# Basic | ||
|
||
- order: 5 | ||
|
||
`<ErrorBoundary>` can be used to avoid blank screen caused by local errors | ||
::: | ||
|
||
--- | ||
|
||
````jsx | ||
import { ConfigProvider, Button } from '@alifd/next'; | ||
|
||
const { ErrorBoundary, config } = ConfigProvider; | ||
|
||
class Demo extends React.Component { | ||
render() { | ||
return ( | ||
<span>{this.props.locale.ok}</span> | ||
); | ||
} | ||
} | ||
|
||
const NewDemo = config(Demo); | ||
|
||
const fallbackUI = (props) => { | ||
const { error, errorInfo } = props; | ||
return <span style={{color: 'red'}}>Error: {error.toString()}</span>; | ||
}; | ||
|
||
export default class App extends React.Component { | ||
state = { | ||
locale: { | ||
ok: 'ok' | ||
} | ||
}; | ||
|
||
onClick = () => { | ||
this.setState({ | ||
locale: undefined | ||
}); | ||
}; | ||
|
||
render() { | ||
return <div> | ||
Pass undefined to locale which will cause an error: <Button type="primary" onClick={this.onClick}>trigger error</Button> | ||
<br/> | ||
<br/> | ||
Default fallback UI: | ||
<hr /> | ||
<ConfigProvider errorBoundary> | ||
<NewDemo locale={this.state.locale}/> | ||
</ConfigProvider> | ||
<br/> | ||
<br/> | ||
Customize fallback UI of configed Component(Basic Components / Biz Components): | ||
<hr /> | ||
<ConfigProvider errorBoundary={{ | ||
fallbackUI: props => { | ||
const { error, errorInfo } = props; | ||
return <span style={{color: 'red'}}>Error: {error.toString()}</span>; | ||
}, | ||
afterCatch: () => { | ||
console.log('catching'); | ||
} | ||
}}> | ||
<NewDemo locale={this.state.locale}/> | ||
</ConfigProvider> | ||
</div> | ||
} | ||
} | ||
|
||
ReactDOM.render(<App />, mountNode); | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
DefaultUI.propTypes = { | ||
error: PropTypes.object, | ||
errorInfo: PropTypes.object, | ||
}; | ||
|
||
function DefaultUI() { | ||
return ''; | ||
} | ||
|
||
export default class ErrorBoundary extends React.Component { | ||
static propTypes = { | ||
children: PropTypes.element, | ||
/** | ||
* 捕获错误后的自定义处理, 比如埋点上传 | ||
* @param {Object} error 错误 | ||
* @param {Object} errorInfo 错误详细信息 | ||
*/ | ||
afterCatch: PropTypes.func, | ||
/** | ||
* 捕获错误后的展现 自定义组件 | ||
* @param {Object} error 错误 | ||
* @param {Object} errorInfo 错误详细信息 | ||
* @returns {Element} 捕获错误后的处理 | ||
*/ | ||
fallbackUI: PropTypes.func, | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { error: null, errorInfo: null }; | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
this.setState({ | ||
error: error, | ||
errorInfo: errorInfo, | ||
}); | ||
|
||
const { afterCatch } = this.props; | ||
|
||
if ('afterCatch' in this.props && typeof afterCatch === 'function') { | ||
this.props.afterCatch(error, errorInfo); | ||
} | ||
} | ||
|
||
render() { | ||
const { fallbackUI: FallbackUI = DefaultUI } = this.props; | ||
|
||
if (this.state.errorInfo) { | ||
return ( | ||
<FallbackUI | ||
error={this.state.error} | ||
errorInfo={this.state.errorInfo} | ||
/> | ||
); | ||
} | ||
// Normally, just render children | ||
return this.props.children; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.