1 | # react-dynamic-import
|
2 |
|
3 | Dynamically load and render any react module(Component or an HOC) using dynamic import 🎉
|
4 |
|
5 | Tiny(**around 1.18kb gzip**) dynamic module loader and renderer.
|
6 |
|
7 | 👉 [DEMO](https://codesandbox.io/s/react-dynamic-import-hooks-demo-108-kuf3u)
|
8 |
|
9 | > ⚠️ Hooks only(requires react 16.8.0 or above), use [v1.0.4](https://www.npmjs.com/package/react-dynamic-import/v/1.0.4) if you want older react versions support
|
10 | >
|
11 | > Dynamic loading of component is already supported in React using React.lazy and Suspense, But, dynamic loading of HOC is tricky and is unsupported in React.
|
12 | >
|
13 | > Should work with any bundler(eg: webpack, parcel etc) which supports dynamic import ✨
|
14 |
|
15 | [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
|
16 | [![Open Source Love](https://badges.frapsoft.com/os/v2/open-source.svg?v=103)](https://github.com/ganapativs/react-dynamic-import/)
|
17 | [![Build Status](https://travis-ci.com/ganapativs/react-dynamic-import.svg?branch=master)](https://travis-ci.com/ganapativs/react-dynamic-import)
|
18 | [![npm version](https://badge.fury.io/js/react-dynamic-import.svg)](https://badge.fury.io/js/react-dynamic-import)
|
19 | [![GitHub version](https://badge.fury.io/gh/ganapativs%2Freact-dynamic-import.svg)](https://badge.fury.io/gh/ganapativs%2Freact-dynamic-import) [![Greenkeeper badge](https://badges.greenkeeper.io/ganapativs/react-dynamic-import.svg)](https://greenkeeper.io/)
|
20 |
|
21 | ## Table of Contents
|
22 |
|
23 | - [Install](#install)
|
24 | - [Basic Usage](#basic-usage)
|
25 | - [Advanced Usage](#advanced-usage)
|
26 | - [API](#api)
|
27 | - [Contribute](#contribute)
|
28 | - [License](#license)
|
29 |
|
30 | ## Install
|
31 |
|
32 | ### NPM
|
33 |
|
34 | ```sh
|
35 | npm install react-dynamic-import
|
36 | ```
|
37 |
|
38 | ### Yarn
|
39 |
|
40 | ```sh
|
41 | yarn add react-dynamic-import
|
42 | ```
|
43 |
|
44 | ### UMD build
|
45 |
|
46 | ```html
|
47 | <script src="https://unpkg.com/react-dynamic-import/dist/react-dynamic-import.umd.js"></script>
|
48 | ```
|
49 |
|
50 | ## Basic usage
|
51 |
|
52 | 1. Component
|
53 |
|
54 | - **Folder structure**
|
55 |
|
56 | ```text
|
57 | |_ realComponent.js
|
58 | |_ container.js <-- working file
|
59 | ```
|
60 |
|
61 | - **Usage**
|
62 |
|
63 | ```js
|
64 | // Import library
|
65 | import ReactDynamicImport from "react-dynamic-import";
|
66 | // or const ReactDynamicImport = require('react-dynamic-import');
|
67 |
|
68 | // Define dynamic import loader function
|
69 | const loader = () => import(`./realComponent.js`);
|
70 |
|
71 | /**
|
72 | * Use dynamic module and lazy fetch component
|
73 | *
|
74 | * Make sure to use it outside render method,
|
75 | * else new component is rendered in each render
|
76 | *
|
77 | * You can choose to show a placeholder and render
|
78 | * error component in case of error, check API section for more
|
79 | */
|
80 | const RealComponent = ReactDynamicImport({ loader });
|
81 |
|
82 | class Container extends React.component {
|
83 | render() {
|
84 | /**
|
85 | * This component is dynamically fetched and rendered
|
86 | * on first usage/render
|
87 | */
|
88 | return <RealComponent />;
|
89 | }
|
90 | }
|
91 | ```
|
92 |
|
93 | 1. HOC
|
94 |
|
95 | - **Folder structure**
|
96 |
|
97 | ```text
|
98 | |_ realComponent.js <-- Real component to wrap in HOC
|
99 | |_ withHOC.js <-- HOC
|
100 | |_ container.js <-- working file
|
101 | ```
|
102 |
|
103 | - **Usage**
|
104 |
|
105 | ```js
|
106 | // Import library
|
107 | import ReactDynamicImport from "react-dynamic-import";
|
108 | // or const ReactDynamicImport = require('react-dynamic-import');
|
109 | import RealComponent from "./realComponent.js";
|
110 |
|
111 | // Define dynamic import loader function
|
112 | const loader = () => import(`./withHOC.js`);
|
113 |
|
114 | /**
|
115 | * Use dynamic module and lazy fetch HOC
|
116 | *
|
117 | * Make sure to use it outside render method,
|
118 | * else new component is rendered in each render
|
119 | *
|
120 | * You can choose to show a placeholder and render error
|
121 | * component in case of error, check API section for more
|
122 | */
|
123 | const DynamicHOC = ReactDynamicImport({ loader, isHOC: true });
|
124 | const WrappedComponent = DynamicHOC(RealComponent);
|
125 |
|
126 | class Container extends React.component {
|
127 | render() {
|
128 | /**
|
129 | * The actual HOC is lazy loaded and executed,
|
130 | * which in turn renders the actual component lazily
|
131 | */
|
132 | return <WrappedComponent />;
|
133 | }
|
134 | }
|
135 | ```
|
136 |
|
137 | ## Advanced usage
|
138 |
|
139 | 1. Component
|
140 |
|
141 | - **Folder structure**
|
142 |
|
143 | ```text
|
144 | |_ dynamic
|
145 | | |_ realComponent-en.js
|
146 | | |_ realComponent-eu.js
|
147 | | |_ realComponent-kn.js
|
148 | |_ container.js <-- working file
|
149 | ```
|
150 |
|
151 | - **Usage**
|
152 |
|
153 | ```js
|
154 | // Import library
|
155 | import ReactDynamicImport from "react-dynamic-import";
|
156 | // or const ReactDynamicImport = require('react-dynamic-import');
|
157 |
|
158 | /**
|
159 | * Define dynamic import loader function
|
160 | *
|
161 | * This loads specific module from many available
|
162 | * modules in the directory, using given module name
|
163 | */
|
164 | const loader = f => import(`./dynamic/${f}.js`);
|
165 |
|
166 | // Use dynamic module and lazy fetch component
|
167 | class Container extends React.component {
|
168 | constructor(props) {
|
169 | super(props);
|
170 |
|
171 | /**
|
172 | * Make sure to use it outside render method, else
|
173 | * new component is rendered in each render
|
174 | *
|
175 | * You can choose to show a placeholder and render error
|
176 | * component in case of error, check API section for more
|
177 | *
|
178 | * This loads different module when different language
|
179 | * configuration is passed
|
180 | */
|
181 | this.RealComponent = ReactDynamicImport({
|
182 | name: `realComponent-${props.lang || "en"}`,
|
183 | loader
|
184 | });
|
185 | }
|
186 |
|
187 | render() {
|
188 | const { RealComponent } = this;
|
189 |
|
190 | /**
|
191 | * This component is dynamically fetched and rendered
|
192 | * on first usage/render
|
193 | */
|
194 | return <RealComponent />;
|
195 | }
|
196 | }
|
197 | ```
|
198 |
|
199 | 1. HOC
|
200 |
|
201 | - **Folder structure**
|
202 |
|
203 | ```text
|
204 | |_ dynamic <-- Dynamic HOC's
|
205 | | |_ withHOC-en.js
|
206 | | |_ withHOC-eu.js
|
207 | | |_ withHOC-kn.js
|
208 | |_ realComponent.js <-- Real component to wrap in HOC
|
209 | |_ container.js <-- working file
|
210 | ```
|
211 |
|
212 | - **Usage**
|
213 |
|
214 | ```js
|
215 | // Import library
|
216 | import ReactDynamicImport from "react-dynamic-import";
|
217 | // or const ReactDynamicImport = require('react-dynamic-import');
|
218 | import RealComponent from "./realComponent.js";
|
219 |
|
220 | /**
|
221 | * Define dynamic import loader function
|
222 | *
|
223 | * This loads specific module from many available
|
224 | * modules in the directory, using given module name
|
225 | */
|
226 | const loader = f => import(`./dynamic/${f}.js`);
|
227 |
|
228 | // Use dynamic module and lazy fetch component
|
229 | class Container extends React.component {
|
230 | constructor(props) {
|
231 | super(props);
|
232 |
|
233 | /**
|
234 | * Make sure to use it outside render method, else
|
235 | * new component is rendered in each render
|
236 | *
|
237 | * You can choose to show a placeholder and render error
|
238 | * component in case of error, check API section for more
|
239 | *
|
240 | * This loads different module when different language
|
241 | * configuration is passed
|
242 | */
|
243 | const DynamicHOC = ReactDynamicImport({
|
244 | name: `withHOC-${props.lang || "en"}`,
|
245 | loader,
|
246 | isHOC: true
|
247 | });
|
248 | this.WrappedComponent = DynamicHOC(RealComponent);
|
249 | }
|
250 |
|
251 | render() {
|
252 | const { WrappedComponent } = this;
|
253 |
|
254 | /**
|
255 | * The actual HOC is lazy loaded and executed,
|
256 | * which in turn renders the actual component lazily
|
257 | */
|
258 | return <WrappedComponent />;
|
259 | }
|
260 | }
|
261 | ```
|
262 |
|
263 | Checkout [API](#api) for more info.
|
264 |
|
265 | ## API
|
266 |
|
267 | - [React Dynamic Import](docs/react-dynamic-import.md)
|
268 |
|
269 | ## Contribute
|
270 |
|
271 | Thanks for taking time to contribute, please read [docs](docs) and checkout [src](src) to understand how things work.
|
272 |
|
273 | ### Reporting Issues
|
274 |
|
275 | Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues).
|
276 | If don't, just open a [new clear and descriptive issue](../../issues/new).
|
277 |
|
278 | ### Submitting pull requests
|
279 |
|
280 | Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.
|
281 |
|
282 | - Fork it!
|
283 | - Clone your fork: `git clone https://github.com/<your-username>/react-dynamic-import`
|
284 | - Navigate to the newly cloned directory: `cd react-dynamic-import`
|
285 | - Create a new branch for the new feature: `git checkout -b my-new-feature`
|
286 | - Install the tools necessary for development: `yarn`
|
287 | - Make your changes.
|
288 | - Commit your changes: `git commit -am 'Add some feature'`
|
289 | - Push to the branch: `git push origin my-new-feature`
|
290 | - Submit a pull request with full remarks documenting your changes
|
291 |
|
292 | ### TODO
|
293 |
|
294 | - [ ] Test cases
|
295 |
|
296 | ## License
|
297 |
|
298 | [MIT License](https://opensource.org/licenses/MIT) © [Ganapati V S](https://meetguns.com)
|