88 */
99
1010import * as React from 'react' ;
11- import { createContext , Component , Fragment , useContext } from 'react' ;
11+ import { createContext , Component , useContext } from 'react' ;
1212import PropTypes from 'prop-types' ;
1313
1414function someNamedFunction ( ) { }
1515
16+ function formatContextForDisplay ( name , value ) {
17+ return (
18+ < li >
19+ { name } : < pre > { JSON . stringify ( value , null , 2 ) } </ pre >
20+ </ li >
21+ ) ;
22+ }
23+
1624const contextData = {
1725 array : [ 'first' , 'second' , 'third' ] ,
1826 bool : true ,
@@ -61,7 +69,7 @@ class LegacyContextConsumer extends Component<any> {
6169 } ;
6270
6371 render ( ) {
64- return null ;
72+ return formatContextForDisplay ( 'LegacyContextConsumer' , this . context ) ;
6573 }
6674}
6775
@@ -88,34 +96,55 @@ class ModernContextType extends Component<any> {
8896 static contextType = ModernContext ;
8997
9098 render ( ) {
91- return null ;
99+ return formatContextForDisplay ( 'ModernContextType' , this . context ) ;
92100 }
93101}
94102
95103function FunctionalContextConsumer ( ) {
96- useContext ( StringContext ) ;
97- return null ;
104+ const value = useContext ( StringContext ) ;
105+ return formatContextForDisplay ( 'FunctionalContextConsumer' , value ) ;
98106}
99107
100108export default function Contexts ( ) {
101109 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 >
120149 ) ;
121150}
0 commit comments