Skip to content

Commit 84d6a44

Browse files
committed
chore: add timeout loading view
1 parent 35f7c8c commit 84d6a44

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/LazyComponent.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Props<T extends ComponentType<any>> = {
2222
loadingColor?: string;
2323
placeholder?: ReactElement;
2424
onReady?: () => void;
25+
timeout?: number;
2526
};
2627

2728
export default function LazyComponent<T extends ComponentType<any>>({
@@ -31,6 +32,7 @@ export default function LazyComponent<T extends ComponentType<any>>({
3132
loadingColor,
3233
placeholder,
3334
onReady,
35+
timeout = 100,
3436
...props
3537
}: Props<T> & ComponentProps<T>) {
3638
const [ready, setReady] = useState(false);
@@ -45,11 +47,13 @@ export default function LazyComponent<T extends ComponentType<any>>({
4547
if (!ref.current) {
4648
ref.current = await savedLoad.current();
4749
}
48-
setReady(true);
49-
if (onReady) {
50-
onReady();
51-
}
52-
}, [onReady]);
50+
setTimeout(() => {
51+
setReady(true);
52+
if (onReady) {
53+
onReady();
54+
}
55+
}, timeout);
56+
}, [onReady, timeout]);
5357

5458
useEffect(() => {
5559
if (visible) {
@@ -83,8 +87,8 @@ export default function LazyComponent<T extends ComponentType<any>>({
8387

8488
const styles = StyleSheet.create({
8589
loadingContainer: {
90+
...StyleSheet.absoluteFillObject,
8691
alignItems: 'center',
8792
justifyContent: 'center',
88-
height: 100,
8993
},
9094
});

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"allowUnreachableCode": false,
88
"allowUnusedLabels": false,
99
"esModuleInterop": true,
10-
"importsNotUsedAsValues": "error",
1110
"forceConsistentCasingInFileNames": true,
1211
"jsx": "react",
1312
"lib": ["esnext"],

0 commit comments

Comments
 (0)