Skip to content

Commit 4de55a3

Browse files
committed
Add code spliting interview questions
1 parent 85c0249 commit 4de55a3

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@
286286
|270| [What are the advantages of formik over redux form library?](#what-are-the-advantages-of-formik-over-redux-form-library)|
287287
|271| [Why do you not required to use inheritance?](#why-do-you-not-required-to-use-inheritance)|
288288
|272| [Can I use web components in react application?](#can-i-use-web-components-in-react-application)|
289+
|273| [What is dynamic import?](#what-is-dynamic-import)|
290+
|274| [What are loadable components?](#what-are-loadable-components)|
291+
|275| [What is suspense component?](#what-is-suspense-component)|
292+
|276| [What is route based code splitting?](#what-is-route-based-code-splitting)|
289293

290294
## Core React
291295

@@ -4523,4 +4527,70 @@
45234527
}
45244528
}
45254529
export default App;
4526-
```
4530+
```
4531+
273. ### What is dynamic import?
4532+
The dynamic import() syntax is a ECMAScript proposal not currently part of the language standard. It is expected to be accepted in the near future. You can achieve code-splitting into your app using dynamic import(). Let's take an example of addition,
4533+
**Normal Import**
4534+
```javascript
4535+
import { add } from './math';
4536+
console.log(add(10, 20));
4537+
```
4538+
**Dynamic Import**
4539+
```javascript
4540+
import("./math").then(math => {
4541+
console.log(math.add(10, 20));
4542+
});
4543+
```
4544+
274. ### What are loadable components?
4545+
If you want to do code-splitting in a server rendered app, it is recommend to use Loadable Components because React.lazy and Suspense is not yet available for server-side rendering. Loadable lets you render a dynamic import as a regular component. Lets take an example,
4546+
```javascript
4547+
import loadable from '@loadable/component'
4548+
4549+
const OtherComponent = loadable(() => import('./OtherComponent'))
4550+
4551+
function MyComponent() {
4552+
return (
4553+
<div>
4554+
<OtherComponent />
4555+
</div>
4556+
)
4557+
}
4558+
```
4559+
Now OtherComponent will be loaded in a separated bundle
4560+
275. ### What is suspense component?
4561+
If the module containing the dynamic import is not yet loaded by the time parent component renders, you must show some fallback content while you’re waiting for it to load using a loading indicator. This can be done using **Suspense** component. For example, the below code uses suspense component,
4562+
```javascript
4563+
const OtherComponent = React.lazy(() => import('./OtherComponent'));
4564+
4565+
function MyComponent() {
4566+
return (
4567+
<div>
4568+
<Suspense fallback={<div>Loading...</div>}>
4569+
<OtherComponent />
4570+
</Suspense>
4571+
</div>
4572+
);
4573+
}
4574+
```
4575+
As mentioned in the above code, Suspense is wrapped above the lazy component.
4576+
276. ### What is route based code splitting?
4577+
One of the best place to do code splitting is with routes. The entire page is going to re-render at once so users are unlikely to interact with other elements in the page at the same time. Due to this, the user experience won't be disturbed. Let us take an example of route based website using libraries like React Router with React.lazy,
4578+
```javascript
4579+
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
4580+
import React, { Suspense, lazy } from 'react';
4581+
4582+
const Home = lazy(() => import('./routes/Home'));
4583+
const About = lazy(() => import('./routes/About'));
4584+
4585+
const App = () => (
4586+
<Router>
4587+
<Suspense fallback={<div>Loading...</div>}>
4588+
<Switch>
4589+
<Route exact path="/" component={Home}/>
4590+
<Route path="/about" component={About}/>
4591+
</Switch>
4592+
</Suspense>
4593+
</Router>
4594+
);
4595+
```
4596+
In the above code, the code splitting will happen at each route level.

0 commit comments

Comments
 (0)