8
8
*/
9
9
10
10
import * as React from 'react' ;
11
- import { createContext , Component , Fragment , useContext } from 'react' ;
11
+ import { createContext , Component , useContext } from 'react' ;
12
12
import PropTypes from 'prop-types' ;
13
13
14
14
function someNamedFunction ( ) { }
15
15
16
+ function formatContextForDisplay ( name , value ) {
17
+ return (
18
+ < li >
19
+ { name } : < pre > { JSON . stringify ( value , null , 2 ) } </ pre >
20
+ </ li >
21
+ ) ;
22
+ }
23
+
16
24
const contextData = {
17
25
array : [ 'first' , 'second' , 'third' ] ,
18
26
bool : true ,
@@ -61,7 +69,7 @@ class LegacyContextConsumer extends Component<any> {
61
69
} ;
62
70
63
71
render ( ) {
64
- return null ;
72
+ return formatContextForDisplay ( 'LegacyContextConsumer' , this . context ) ;
65
73
}
66
74
}
67
75
@@ -88,34 +96,55 @@ class ModernContextType extends Component<any> {
88
96
static contextType = ModernContext ;
89
97
90
98
render ( ) {
91
- return null ;
99
+ return formatContextForDisplay ( 'ModernContextType' , this . context ) ;
92
100
}
93
101
}
94
102
95
103
function FunctionalContextConsumer ( ) {
96
- useContext ( StringContext ) ;
97
- return null ;
104
+ const value = useContext ( StringContext ) ;
105
+ return formatContextForDisplay ( 'FunctionalContextConsumer' , value ) ;
98
106
}
99
107
100
108
export default function Contexts ( ) {
101
109
return (
102
- < Fragment >
103
- < LegacyContextProvider >
104
- < LegacyContextConsumer />
105
- </ LegacyContextProvider >
106
- < ModernContext . Provider value = { contextData } >
107
- < ModernContext . Consumer > { value => null } </ ModernContext . Consumer >
108
- < ModernContextType />
109
- </ ModernContext . Provider >
110
- < FunctionalContextConsumer />
111
- < ArrayContext . Consumer > { value => null } </ ArrayContext . Consumer >
112
- < BoolContext . Consumer > { value => null } </ BoolContext . Consumer >
113
- < FuncContext . Consumer > { value => null } </ FuncContext . Consumer >
114
- < NumberContext . Consumer > { value => null } </ NumberContext . Consumer >
115
- < StringContext . Consumer > { value => null } </ StringContext . Consumer >
116
- < SymbolContext . Consumer > { value => null } </ SymbolContext . Consumer >
117
- < NullContext . Consumer > { value => null } </ NullContext . Consumer >
118
- < UndefinedContext . Consumer > { value => null } </ UndefinedContext . Consumer >
119
- </ Fragment >
110
+ < div >
111
+ < h1 > Contexts</ h1 >
112
+ < ul >
113
+ < LegacyContextProvider >
114
+ < LegacyContextConsumer />
115
+ </ LegacyContextProvider >
116
+ < ModernContext . Provider value = { contextData } >
117
+ < ModernContext . Consumer >
118
+ { value => formatContextForDisplay ( 'ModernContext.Consumer' , value ) }
119
+ </ ModernContext . Consumer >
120
+ < ModernContextType />
121
+ </ ModernContext . Provider >
122
+ < FunctionalContextConsumer />
123
+ < ArrayContext . Consumer >
124
+ { value => formatContextForDisplay ( 'ArrayContext.Consumer' , value ) }
125
+ </ ArrayContext . Consumer >
126
+ < BoolContext . Consumer >
127
+ { value => formatContextForDisplay ( 'BoolContext.Consumer' , value ) }
128
+ </ BoolContext . Consumer >
129
+ < FuncContext . Consumer >
130
+ { value => formatContextForDisplay ( 'FuncContext.Consumer' , value ) }
131
+ </ FuncContext . Consumer >
132
+ < NumberContext . Consumer >
133
+ { value => formatContextForDisplay ( 'NumberContext.Consumer' , value ) }
134
+ </ NumberContext . Consumer >
135
+ < StringContext . Consumer >
136
+ { value => formatContextForDisplay ( 'StringContext.Consumer' , value ) }
137
+ </ StringContext . Consumer >
138
+ < SymbolContext . Consumer >
139
+ { value => formatContextForDisplay ( 'SymbolContext.Consumer' , value ) }
140
+ </ SymbolContext . Consumer >
141
+ < NullContext . Consumer >
142
+ { value => formatContextForDisplay ( 'NullContext.Consumer' , value ) }
143
+ </ NullContext . Consumer >
144
+ < UndefinedContext . Consumer >
145
+ { value => formatContextForDisplay ( 'UndefinedContext.Consumer' , value ) }
146
+ </ UndefinedContext . Consumer >
147
+ </ ul >
148
+ </ div >
120
149
) ;
121
150
}
0 commit comments