1- import React , { useState , useEffect } from "react" ;
1+ import React , { useState , useEffect , ErrorInfo } from "react" ;
22import ReactDOM from "react-dom" ;
33import { Test } from "./components" ;
44import { TextField } from "./components/text-field" ;
@@ -10,7 +10,7 @@ import "./reset.scss";
1010import "./app.scss" ;
1111
1212const store = new GroupStoreMutable ( ) ;
13- const App = ( ) => {
13+ const Test = ( ) => {
1414 const forceUpdate = useForceUpdate ( ) ;
1515 useEffect ( ( ) => {
1616 const listener = ( ) => {
@@ -34,18 +34,22 @@ const App = () => {
3434 < div className = "forms" >
3535 < GroupContext . Provider value = { groupContext } >
3636 < div >
37+ < button onClick = { ( ) => setShow ( ! show ) } >
38+ { ! show ? "Mount" : "Unmount" }
39+ </ button >
3740 < label >
38- Imported:
41+ Last name
3942 { show ? (
4043 < TextField
41- name = "firstName "
42- initialValue = "Test "
44+ name = "lastName "
45+ initialValue = "Smith "
4346 />
4447 ) : null }
4548 </ label >
46- < button onClick = { ( ) => setShow ( ! show ) } >
47- { ! show ? "Mount" : "Unmount" }
48- </ button >
49+ < label >
50+ First name
51+ < TextField name = "firstName" initialValue = "John" />
52+ </ label >
4953 </ div >
5054 </ GroupContext . Provider >
5155 </ div >
@@ -72,4 +76,43 @@ const App = () => {
7276 ) ;
7377} ;
7478
79+ interface State {
80+ error ?: Error ;
81+ info ?: ErrorInfo ;
82+ }
83+
84+ class App extends React . Component {
85+ public state : State = { } ;
86+
87+ public componentDidCatch ( error : Error , info : ErrorInfo ) {
88+ this . setState ( ( ) => ( {
89+ error : error ,
90+ info : info
91+ } ) ) ;
92+ }
93+
94+ public render ( ) : JSX . Element {
95+ const { error } = this . state ;
96+ if ( error != null ) {
97+ const info = this . state . info ! ;
98+ console . warn ( error . message ) ;
99+ console . log ( info ) ;
100+ return (
101+ < div className = "error" >
102+ < p className = "disclaimer" >
103+ Oops! An error has occurred! Here’s what we know…
104+ </ p >
105+ < p className = "message" > { error . message } </ p >
106+ < p className = "component-stack" >
107+ Component stack:
108+ < pre > { info . componentStack . substr ( 1 ) } </ pre >
109+ </ p >
110+ </ div >
111+ ) ;
112+ }
113+
114+ return < Test /> ;
115+ }
116+ }
117+
75118ReactDOM . render ( < App /> , document . getElementById ( "root" ) ) ;
0 commit comments