maintain history state when refreshing from error page

This commit is contained in:
Sean Yesmunt 2019-05-10 12:43:11 -04:00
parent a8b6f25c44
commit 288b8d84df

View file

@ -3,19 +3,25 @@ import { Lbryio } from 'lbryinc';
import * as React from 'react'; import * as React from 'react';
import Yrbl from 'component/yrbl'; import Yrbl from 'component/yrbl';
import Button from 'component/button'; import Button from 'component/button';
import { withRouter } from 'react-router';
type Props = { type Props = {
children: React.Node, children: React.Node,
history: {
replace: string => void,
},
}; };
type State = { type State = {
hasError: boolean, hasError: boolean,
}; };
export default class ErrorBoundary extends React.Component<Props, State> { class ErrorBoundary extends React.Component<Props, State> {
constructor() { constructor() {
super(); super();
this.state = { hasError: false }; this.state = { hasError: false };
(this: any).refresh = this.refresh.bind(this);
} }
static getDerivedStateFromError() { static getDerivedStateFromError() {
@ -35,6 +41,13 @@ export default class ErrorBoundary extends React.Component<Props, State> {
} }
} }
refresh() {
const { history } = this.props;
// use history.replace instead of history.push so the user can't click back to the errored page
history.replace('');
this.setState({ hasError: false });
}
render() { render() {
if (this.state.hasError) { if (this.state.hasError) {
return ( return (
@ -50,7 +63,7 @@ export default class ErrorBoundary extends React.Component<Props, State> {
button="link" button="link"
className="load-screen__button" className="load-screen__button"
label={__('refreshing the app')} label={__('refreshing the app')}
onClick={() => (window.location.href = '/')} onClick={this.refresh}
/>{' '} />{' '}
{__('to fix it')}. {__('to fix it')}.
</p> </p>
@ -64,3 +77,5 @@ export default class ErrorBoundary extends React.Component<Props, State> {
return this.props.children; return this.props.children;
} }
} }
export default withRouter(ErrorBoundary);