You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04-suspense-and-async-mode/README.md
+46-39
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,13 @@
2
2
3
3
## Code Splitting
4
4
5
-
* Bundling your application is great, but as your app grows, your bundle size will grow too, especially if you are including large 3rd party libraries
6
-
* This may have a huge impact on the time to first load and UX
7
-
* The bundle can be split into smaller chunks where the most important ones can be loaded first and then every other secondary are lazily loaded, so called**code splitting**
5
+
* Bundling your application is great, but as your app grows, your bundle size will grow too, especially if you include large 3rd party libraries
6
+
* This may have a huge impact on the time to first load and UX, especially on slow networks
7
+
* The bundle can be split into smaller chunks where the most important ones can be loaded first and then every other secondary are lazily loaded -**code splitting**
8
8
9
9
### What Is Code Splitting
10
10
11
11
* Multiple bundles are created that are loaded dynamically, only at the time they are needed
12
-
* Enables to **lazy-load** just the components that are currently needed by the user
13
12
* Can significantly improve performance of your app
14
13
15
14
### React.lazy
@@ -48,10 +47,11 @@ const MyComponent = () => (
48
47
);
49
48
```
50
49
51
-
* lazy() Takes a function that calls the dynamic `import()`=> it must return a Promise which resolves to a module containing a React component
50
+
* lazy() Takes a function that calls the dynamic `import()`- that must return a Promise which resolves to a module containing React component
52
51
* Using lazy loaded component on its own **will throw an error**, we have to wrap it in Suspense component
53
-
* But besides that we can use lazy loaded component the same way we use standard components
54
-
* NOTE: React.lazy supports only default exports, if you want to use named exports, you must use intermediate module that reexports it as the default
52
+
* But besides that, we can use lazy loaded component the same way we use standard components
53
+
* React.lazy supports only default exports, if you want to use named exports, you must use intermediate module that reexports it as the default
54
+
* lazy() and Suspense is [not yet available for server-side rendering](https://reactjs.org/docs/code-splitting.html#reactlazy)
55
55
56
56
### Suspense
57
57
@@ -72,8 +72,8 @@ const MyComponent = () => (
72
72
```
73
73
74
74
* You can place the `Suspense` anywhere above the lazy loaded component
75
-
* It doesn't matter how deep is the lazy loaded component nested, it works similar to try-catch syntax - first Suspense component up in the component tree will catch the dynamic content and display fallback
76
-
* You can wrap multiple lazy components inside a single `Suspense`component=> it will display fallback until all dynamic components are loaded. Try to **avoid multiple spinners next to each other**
75
+
* It doesn't matter how deep the lazy loaded component is nested, it works similar to try-catch syntax - first Suspense component up in the component tree will catch the dynamic content and display fallback
76
+
* You can wrap multiple lazy components inside a single `Suspense`component=> it will display fallback until all nested dynamic components are loaded. Try to **avoid multiple spinners next to each other**
React.Suspense has a componentDidCatch sort of mechanism which will catch the promise thrown by `ImageResource.read()` and show a fallback until the promise is resolved
165
+
* React.Suspense has a componentDidCatch sort of mechanism which will catch the promise thrown by `ImageResource.read()` and show a fallback until the promise is resolved.
166
+
* To extend this to include an API call to get the URL of an image is easy, let's try it:
164
167
165
-
> Demo
168
+
> Exercise 2 => [Use Suspense with data fetch and images](./exercise-fetch/src/pages/kitties.jsx)
166
169
167
170
## Concurrent Mode (Asynchronous Rendering)
168
171
169
-
> "Concurrent Mode lets React apps be more responsive by rendering component trees without blocking the main thread." - Dan Abramov
172
+
"Concurrent Mode lets React apps be more responsive by rendering component trees without blocking the main thread." - Dan Abramov
> [Source: Andrew Clark's twitter post](https://twitter.com/acdlite/status/977291318324948992)
170
176
171
-
* Formerly known as "Async mode", but "Concurrent mode" has been chosen eventually because it reflects more the ability to perform work on different priority levels
177
+
* Formerly known as "Async mode", but "Concurrent mode" name has been chosen eventually because it reflects more the ability to perform work on different priority levels
172
178
* Allows React to interrupt a long-running render (for example, rendering a new feed story) to handle a high-priority event (for example, text input or hover)
173
-
* keeps your app responsive while rendering complex component trees
174
-
* Also improves the user experience of Suspense by skipping unnecessary loading states on fast connections
175
-
* Concurrent mode is the future of React, so far only in experimental state.
179
+
* Keeps your app responsive while rendering complex component trees
180
+
* Allows Suspense to skip unnecessary loading states on fast connections (flashing spinners)
181
+
* Concurrent mode is the future of React, but so far only in experimental state.
176
182
* In Road map planned for Q2 2019
177
183
* No documentation yet
178
184
179
-
What's possible in Async mode?E.g. skipping unnecessary loading states on fast connections when using Suspense.
180
-
When we use lazy loaded components, small delay in load is not noticeable by user. There is a threshold -if the component load is delayed, but it still loads "fast enough", then it's counterproductive to display the spinner - it can harm **perceived** performance of the app.
185
+
If you want to know more about how the concurrent mode is implemented, check out `fiber` and `algebraic effects`
maxDuration prop of Suspense in concurrent mode defines the time in ms after which our fallback component will show up. This will avoid screen flickering issue which usually occurs on faster network where the loader shows up for few ms and then the data comes immediately.
203
+
This will allow to use new features, e.g. maxDuration prop of Suspense - it defines the time in ms after which our fallback component will show up. This will avoid screen flickering issue which usually occurs on faster network where the loader shows up for few ms and then the data comes immediately.
197
204
198
205
### Important Changes In React With Concurrent Mode
199
206
200
-
Use `<React.StrictMode>` to reveal potential problems after enabling concurrent mode - [see more here](https://reactjs.org/docs/strict-mode.html#identifying-unsafe-lifecycles)
207
+
As render can be called multiple times in Concurrent Mode, some methods are no longer safe to use. Wrap your application in `<React.StrictMode>` to reveal potential problems - [see more here](https://reactjs.org/docs/strict-mode.html#identifying-unsafe-lifecycles)
201
208
202
-
Following lifecycle methods will be deprecated in future versions of React and can be problematic with Concurrent Rendering:
209
+
Following lifecycle methods will be deprecated in future:
203
210
204
211
* `componentWillMount`
205
212
* `componentWillReceiveProps`
@@ -211,20 +218,20 @@ Two new lifecycle methods will be added as a replacement:
> [Source: Dan Abramov's twitter post](https://twitter.com/dan_abramov/status/981712092611989509/photo/1)
214
222
215
-
## Notes
216
-
217
-
* lazy() and Suspense is [not yet available for server-side rendering](https://reactjs.org/docs/code-splitting.html#reactlazy)
218
-
219
-
## Conclusion
220
-
221
-
* Great solution for code splitting and improving UX
222
-
* Concurrent mode in early development so far, but it is the future of React
223
+
***It's crucial to have your render methods/functions pure, without any side effects!***
223
224
224
225
## Sources and Further Reading
225
226
226
-
* [Code Splitting the React app - official documentation]([https://reactjs.org/docs/hooks-intro.html](https://reactjs.org/docs/code-splitting.html))
227
+
### Talks
228
+
227
229
* [Andrew Clark's talk on React Suspense](https://www.youtube.com/watch?v=z-6JC0_cOns)
228
230
* [Dan Abramov's talk on React Suspense](https://www.youtube.com/watch?v=6g3g0Q_XVb4) from ReactFest
229
231
* [Jared Palmer's talk on React Suspense](https://www.youtube.com/watch?v=SCQgE4mTnjU) from React Conf 2018
230
-
* [Talk of Andrew Clark and Brian Vaughn on Concurrent Rendering in React](https://www.youtube.com/watch?v=ByBPyMBTzM0) from React Conf 2018
232
+
* [Talk of Andrew Clark and Brian Vaughn on Concurrent Rendering in React](https://www.youtube.com/watch?v=ByBPyMBTzM0) from React Conf 2018
233
+
234
+
### Articles And Documentation
235
+
236
+
* [Code Splitting the React app - official documentation]([https://reactjs.org/docs/hooks-intro.html](https://reactjs.org/docs/code-splitting.html))
237
+
* [Marvin Frachet - React-cache, time slicing, and fetching with a synchronous API](https://medium.freecodecamp.org/react-cache-time-slicing-and-fetching-with-a-synchronous-api-2a57dc9c2e6d) => advanced reading on how is the concurrent mode implemented
0 commit comments