-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use react-dom createRoot; create ErrorBoundary component
- Loading branch information
1 parent
7641b12
commit 68bf140
Showing
10 changed files
with
103 additions
and
36 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
import React, { PureComponent, ReactNode } from 'react'; | ||
|
||
interface Props { | ||
children?: ReactNode; | ||
} | ||
|
||
interface State { | ||
error: Error | null; | ||
errorInfo: { componentStack: string } | null; | ||
} | ||
|
||
export default class ErrorBoundary extends PureComponent<Props, State> { | ||
constructor(props: Props) { | ||
super(props); | ||
|
||
this.state = { | ||
error: null, | ||
errorInfo: null, | ||
}; | ||
} | ||
|
||
componentDidCatch( | ||
error: Error, | ||
errorInfo: { componentStack: string } | ||
): void { | ||
// Catch errors in any components below and re-render with error message | ||
this.setState({ error, errorInfo }); | ||
|
||
// You can also log error messages to an error reporting service here | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
const { error, errorInfo } = this.state; | ||
|
||
if (!error) { | ||
return children; | ||
} | ||
|
||
return ( | ||
<div> | ||
<h1>Something went wrong</h1> | ||
{error ? <p>{error.toString()}</p> : null} | ||
</div> | ||
); | ||
} | ||
} |
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 @@ | ||
export { default as ErrorBoundary } from './ErrorBoundary/ErrorBoundary'; |
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 |
---|---|---|
@@ -1,26 +1,27 @@ | ||
import './styles/index.css'; | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { Provider as ReduxProvider } from 'react-redux'; | ||
|
||
import configureAppStore, { getPreloadedState } from './store/configureStore'; | ||
|
||
import AppContextProvider from './contexts/AppContextProvider'; | ||
|
||
import { DemoPage, RootPage } from './pages'; | ||
import { DemoPage, HomePage } from './pages'; | ||
|
||
(async () => { | ||
const preloadedState = getPreloadedState(); | ||
|
||
ReactDOM.render( | ||
const root = createRoot(document.getElementById('root')); | ||
|
||
root.render( | ||
<React.StrictMode> | ||
<ReduxProvider store={configureAppStore(preloadedState)}> | ||
<AppContextProvider> | ||
<DemoPage /> | ||
</AppContextProvider> | ||
</ReduxProvider> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
</React.StrictMode> | ||
); | ||
})(); |
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,13 @@ | ||
import React from 'react'; | ||
import { ErrorBoundary } from '../../components'; | ||
|
||
const HomePage = () => { | ||
return ( | ||
<ErrorBoundary> | ||
<h1>React Redux Boilerplate</h1> | ||
<p>You can put the components of your app here</p> | ||
</ErrorBoundary> | ||
); | ||
}; | ||
|
||
export default HomePage; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { default as RootPage } from './Root/RootPage'; | ||
export { default as HomePage } from './Home/HomePage'; | ||
export { default as DemoPage } from './Demo/DemoPage'; |
Empty file.