1
+ {
2
+ "name" : " useLazy" ,
3
+ "description" : " A powerful React hook that enhances React.lazy with loading states, error handling, preloading capabilities, and manual control over component loading. Perfect for code splitting and performance optimization." ,
4
+ "category" : " performance" ,
5
+ "version" : " 1.0.0" ,
6
+ "parameters" : [
7
+ {
8
+ "name" : " importFn" ,
9
+ "type" : " () => Promise<{ default: T } | T>" ,
10
+ "optional" : false ,
11
+ "description" : " Function that returns a promise resolving to the component to be lazy loaded"
12
+ },
13
+ {
14
+ "name" : " options" ,
15
+ "type" : " UseLazyOptions" ,
16
+ "optional" : true ,
17
+ "default" : " {}" ,
18
+ "description" : " Configuration options for the lazy loading behavior" ,
19
+ "properties" : [
20
+ {
21
+ "name" : " preload" ,
22
+ "type" : " boolean" ,
23
+ "optional" : true ,
24
+ "description" : " Whether to preload the component immediately"
25
+ },
26
+ {
27
+ "name" : " onLoadStart" ,
28
+ "type" : " () => void" ,
29
+ "optional" : true ,
30
+ "description" : " Callback when component starts loading"
31
+ },
32
+ {
33
+ "name" : " onLoadSuccess" ,
34
+ "type" : " (component: ComponentType<any>) => void" ,
35
+ "optional" : true ,
36
+ "description" : " Callback when component loads successfully"
37
+ },
38
+ {
39
+ "name" : " onLoadError" ,
40
+ "type" : " (error: Error) => void" ,
41
+ "optional" : true ,
42
+ "description" : " Callback when component fails to load"
43
+ }
44
+ ]
45
+ }
46
+ ],
47
+ "returnType" : {
48
+ "type" : " UseLazyReturn<T>" ,
49
+ "properties" : [
50
+ {
51
+ "name" : " Component" ,
52
+ "type" : " T | null" ,
53
+ "description" : " The lazy component ready to be rendered" ,
54
+ "category" : " state"
55
+ },
56
+ {
57
+ "name" : " loading" ,
58
+ "type" : " boolean" ,
59
+ "description" : " Whether the component is currently loading" ,
60
+ "category" : " state"
61
+ },
62
+ {
63
+ "name" : " error" ,
64
+ "type" : " Error | null" ,
65
+ "description" : " Loading error if any occurred" ,
66
+ "category" : " state"
67
+ },
68
+ {
69
+ "name" : " load" ,
70
+ "type" : " () => Promise<T | null>" ,
71
+ "description" : " Manually trigger component loading" ,
72
+ "category" : " action"
73
+ },
74
+ {
75
+ "name" : " preload" ,
76
+ "type" : " () => Promise<T | null>" ,
77
+ "description" : " Preload the component without rendering it" ,
78
+ "category" : " action"
79
+ },
80
+ {
81
+ "name" : " reset" ,
82
+ "type" : " () => void" ,
83
+ "description" : " Reset the loading state and clear cached component" ,
84
+ "category" : " action"
85
+ }
86
+ ]
87
+ },
88
+ "examples" : [
89
+ {
90
+ "title" : " Basic Lazy Loading" ,
91
+ "description" : " Simple lazy loading with Suspense boundary" ,
92
+ "code" : " import { Suspense } from 'react';\n import { useLazy } from './use-lazy';\n\n function App() {\n const { Component } = useLazy(() => import('./HeavyComponent'));\n\n return (\n <Suspense fallback={<div>Loading...</div>}>\n <Component />\n </Suspense>\n );\n }"
93
+ },
94
+ {
95
+ "title" : " With Loading States" ,
96
+ "description" : " Manual control over loading with loading states" ,
97
+ "code" : " const { Component, loading, error, load } = useLazy(\n () => import('./Dashboard'),\n {\n onLoadStart: () => console.log('Loading started'),\n onLoadSuccess: () => console.log('Component loaded'),\n onLoadError: (err) => console.error('Load failed:', err)\n }\n );\n\n if (error) return <div>Error: {error.message}</div>;\n if (loading) return <div>Loading component...</div>;\n\n return (\n <div>\n <button onClick={load}>Load Dashboard</button>\n {Component && <Component />}\n </div>\n );"
98
+ },
99
+ {
100
+ "title" : " Preloading Components" ,
101
+ "description" : " Preload components for better performance" ,
102
+ "code" : " const { Component, preload } = useLazy(\n () => import('./ExpensiveChart'),\n { preload: true } // Preload immediately\n );\n\n // Or preload on user interaction\n const handleMouseEnter = () => {\n preload(); // Preload on hover\n };\n\n return (\n <div onMouseEnter={handleMouseEnter}>\n <Suspense fallback={<ChartSkeleton />}>\n <Component />\n </Suspense>\n </div>\n );"
103
+ },
104
+ {
105
+ "title" : " Conditional Lazy Loading" ,
106
+ "description" : " Load components based on conditions" ,
107
+ "code" : " const { Component, load, loading } = useLazy(\n () => import('./AdminPanel')\n );\n\n const [showAdmin, setShowAdmin] = useState(false);\n\n const handleShowAdmin = async () => {\n setShowAdmin(true);\n await load(); // Ensure component is loaded\n };\n\n return (\n <div>\n <button onClick={handleShowAdmin} disabled={loading}>\n {loading ? 'Loading...' : 'Show Admin Panel'}\n </button>\n {showAdmin && Component && (\n <Suspense fallback={<div>Loading admin...</div>}>\n <Component />\n </Suspense>\n )}\n </div>\n );"
108
+ }
109
+ ],
110
+ "dependencies" : [" react" ],
111
+ "imports" : [
112
+ " import { lazy, useState, useCallback, useRef, ComponentType } from 'react';"
113
+ ],
114
+ "notes" : [
115
+ " Always wrap lazy components with Suspense boundary for proper loading states" ,
116
+ " The hook caches loaded components to prevent re-loading on re-renders" ,
117
+ " Preloading is useful for components that will likely be needed soon" ,
118
+ " Error handling should be implemented both in the hook callbacks and with Error Boundaries" ,
119
+ " The Component returned is compatible with React.lazy and Suspense"
120
+ ]
121
+ }
0 commit comments