File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import React from 'react'
22import { isFunction } from '../util'
3- import { AsyncState } from '../index'
4- import { getAsyncState } from './asyncState'
53
64type LoadingFunction = ( ) => React . ReactElement < any , any > | null
75type SuccessFunction < Data > = ( data : Data ) => React . ReactElement < any , any > | null
@@ -27,23 +25,16 @@ const AsyncView = <Data, Error>(
2725 renderSuccess,
2826 renderError = null ,
2927 } = props
30- const asyncState = getAsyncState ( data , error )
31-
32- switch ( asyncState ) {
33- case AsyncState . FETCHING :
34- return isFunction ( renderLoading ) ? renderLoading ( ) : < > { renderLoading } </ >
35- case AsyncState . FINISHED_WITH_SUCCESS :
36- return isFunction ( renderSuccess ) ? (
37- renderSuccess ( data as Data )
38- ) : (
39- < > { renderSuccess } </ >
40- )
41- case AsyncState . FINISHED_WITH_ERROR :
42- return isFunction ( renderError ) ? (
43- renderError ( error as Error )
44- ) : (
45- < > { renderError } </ >
46- )
28+ if ( error != null && error != undefined ) {
29+ return isFunction ( renderError ) ? renderError ( error ) : < > { renderError } </ >
30+ } else if ( data != null && data != undefined ) {
31+ return isFunction ( renderSuccess ) ? (
32+ renderSuccess ( data )
33+ ) : (
34+ < > { renderSuccess } </ >
35+ )
36+ } else {
37+ return isFunction ( renderLoading ) ? renderLoading ( ) : < > { renderLoading } </ >
4738 }
4839}
4940
Original file line number Diff line number Diff line change 1+ /**
2+ * @deprecated Consider directly checking data and error values instead.
3+ * Using `AsyncState` leads to unnecessary complexity and may disable type
4+ * inference.
5+ */
16enum AsyncState {
27 FETCHING ,
38 FINISHED_WITH_ERROR ,
49 FINISHED_WITH_SUCCESS ,
510}
611
12+ /**
13+ * @deprecated Consider directly checking data and error values instead.
14+ * Using `AsyncState` leads to unnecessary complexity and may disable type
15+ * inference.
16+ */
717const getAsyncState = ( data : unknown , error : unknown ) : AsyncState => {
818 if ( error != null && error != undefined ) {
919 return AsyncState . FINISHED_WITH_ERROR
You can’t perform that action at this time.
0 commit comments