Skip to content

Commit 8118d26

Browse files
feat: added a React Router example
1 parent b1290c6 commit 8118d26

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/utils/data/concepts.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ codeSnippet:
88
import MessageForm from './MessageForm';
99
import EmojiPicker from './EmojiPicker';
1010
11-
const Chat = () => {
11+
const Chats = () => {
1212
...
1313
return (
1414
<div>
@@ -38,7 +38,7 @@ import MessageForm from './MessageForm';
3838
3939
const EmojiPicker = lazy(() => import('./EmojiPicker'));
4040
41-
const Chat = () => {
41+
const Chats = () => {
4242
...
4343
return (
4444
<div>
@@ -64,6 +64,39 @@ The \`React.lazy\` function provides a built-in way to separate components in an
6464
linesInPrimary: [1, 5, 14, 16]
6565
},
6666
{
67+
id: 'route-based-code-splitting',
68+
optionName: 'Route-based code-splitting',
69+
codeSnippet:
70+
`import React, { lazy, Suspense } from 'react';
71+
import { Switch, Route } from 'react-router-dom';
72+
73+
const Home = lazy(() => import(/* webpackChunkName: "home" */ './Home'));
74+
const Chats = lazy(() => import(/* webpackChunkName: "chats" */ './Chats'));
75+
const Calls = lazy(() => import(/* webpackChunkName: "calls" */ './Calls'));
76+
const Contacts = lazy(() => import(/* webpackChunkName: "contacts" */ './Contacts'));
77+
78+
const App = () => {
79+
<Suspense fallback={<div>Loading...</div>}>
80+
<Switch>
81+
<Route exact path="/" component={Home} />
82+
<Route path="/pages/chats" component={Chats} />
83+
<Route path="/pages/calls" component={Calls} />
84+
<Route path="/pages/contacts" component={Contacts} />
85+
</Switch>
86+
</Suspense>
87+
};`,
88+
textDescription:
89+
`
90+
Deciding where in your app to introduce code splitting can be a bit tricky. You want to make sure you choose places that will split bundles evenly, but won’t disrupt the user experience.
91+
A good place to start is with routes. Most people on the web are used to page transitions taking some amount of time to load. You also tend to be re-rendering the entire page at once so your users are unlikely to be interacting with other elements on the page at the same time.
92+
93+
#### References:
94+
* [(React docs) Code-Splitting / Route-based code splitting](https://reactjs.org/docs/code-splitting.html#route-based-code-splitting)
95+
* [(developers.google) Reduce JavaScript Payloads with Code Splitting](https://developers.google.com/web/fundamentals/performance/optimizing-javascript/code-splitting/)
96+
`,
97+
linesInPrimary: [1, 2, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17]
98+
},
99+
{
67100
id: 'dynamic-with-error-boundary',
68101
optionName: 'Dynamic w/Error Boundary',
69102
codeSnippet:
@@ -92,7 +125,7 @@ class ErrorBoundary extends Component {
92125
}
93126
}
94127
95-
const Chat = () => {
128+
const Chats = () => {
96129
...
97130
return (
98131
<ErrorBoundary>
@@ -127,7 +160,7 @@ import Messages from './Messages';
127160
import MessageForm from './MessageForm';
128161
import ErrorBoundary from './ErrorBoundary';
129162
130-
const Chat = () => {
163+
const Chats = () => {
131164
const [emojiPickerComp, setEmojiPickerComp] = useState(null);
132165
133166
const openEmojiPickerHandler = () => {
@@ -174,7 +207,7 @@ const EmojiPicker = loadable(() => import('./EmojiPicker'), {
174207
fallback: <p>Loading...</p>
175208
});
176209
177-
const Chat = () => {
210+
const Chats = () => {
178211
...
179212
return (
180213
<ErrorBoundary>
@@ -214,7 +247,7 @@ const EmojiPicker = loadableVisibility(() => import('./EmojiPicker'), {
214247
fallback: <p>Loading...</p>
215248
});
216249
217-
const Chat = () => {
250+
const Chats = () => {
218251
...
219252
return (
220253
<div>
@@ -247,7 +280,7 @@ import MessageForm from './MessageForm';
247280
248281
const EmojiPicker = lazy(() => import(/* webpackPrefetch: true */ './EmojiPicker'));
249282
250-
const Chat = () => {
283+
const Chats = () => {
251284
...
252285
return (
253286
<div>
@@ -285,7 +318,7 @@ import MessageForm from './MessageForm';
285318
286319
const EmojiPicker = lazy(() => import(/* webpackPreload: true */ './EmojiPicker'));
287320
288-
const Chat = () => {
321+
const Chats = () => {
289322
...
290323
return (
291324
<div>

0 commit comments

Comments
 (0)