@@ -66,7 +66,23 @@ export const createInstance = <T extends {}>(
66
66
defaultProps : AsyncProps < T > = { } ,
67
67
displayName = "Async"
68
68
) : AsyncConstructor < T > => {
69
- const { Consumer, Provider } = React . createContext < AsyncState < T > > ( undefined as any )
69
+ const { Consumer : UnguardedConsumer , Provider } = React . createContext < AsyncState < T > | undefined > (
70
+ undefined
71
+ )
72
+ function Consumer ( { children } : { children : ( value : AsyncState < T > ) => React . ReactNode } ) {
73
+ return (
74
+ < UnguardedConsumer >
75
+ { value => {
76
+ if ( ! value ) {
77
+ throw new Error (
78
+ "this component should only be used within an associated <Async> component!"
79
+ )
80
+ }
81
+ return children ( value )
82
+ } }
83
+ </ UnguardedConsumer >
84
+ )
85
+ }
70
86
71
87
type Props = AsyncProps < T >
72
88
@@ -266,19 +282,19 @@ export const createInstance = <T extends {}>(
266
282
if ( propTypes ) ( Async as React . ComponentClass ) . propTypes = propTypes . Async
267
283
268
284
const AsyncInitial : AsyncConstructor < T > [ "Initial" ] = props => (
269
- < Consumer > { ( st : AsyncState < T > ) => < IfInitial { ...props } state = { st } /> } </ Consumer >
285
+ < Consumer > { st => < IfInitial { ...props } state = { st } /> } </ Consumer >
270
286
)
271
287
const AsyncPending : AsyncConstructor < T > [ "Pending" ] = props => (
272
- < Consumer > { ( st : AsyncState < T > ) => < IfPending { ...props } state = { st } /> } </ Consumer >
288
+ < Consumer > { st => < IfPending { ...props } state = { st } /> } </ Consumer >
273
289
)
274
290
const AsyncFulfilled : AsyncConstructor < T > [ "Fulfilled" ] = props => (
275
- < Consumer > { ( st : AsyncState < T > ) => < IfFulfilled { ...props } state = { st } /> } </ Consumer >
291
+ < Consumer > { st => < IfFulfilled { ...props } state = { st } /> } </ Consumer >
276
292
)
277
293
const AsyncRejected : AsyncConstructor < T > [ "Rejected" ] = props => (
278
- < Consumer > { ( st : AsyncState < T > ) => < IfRejected { ...props } state = { st } /> } </ Consumer >
294
+ < Consumer > { st => < IfRejected { ...props } state = { st } /> } </ Consumer >
279
295
)
280
296
const AsyncSettled : AsyncConstructor < T > [ "Settled" ] = props => (
281
- < Consumer > { ( st : AsyncState < T > ) => < IfSettled { ...props } state = { st } /> } </ Consumer >
297
+ < Consumer > { st => < IfSettled { ...props } state = { st } /> } </ Consumer >
282
298
)
283
299
284
300
AsyncInitial . displayName = `${ displayName } .Initial`
0 commit comments