Skip to content

Commit ffd2c01

Browse files
committed
Add React lifecycle page
1 parent c1d855f commit ffd2c01

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

lib/components_guide_web/controllers/react_typescript_controller.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule ComponentsGuideWeb.ReactTypescriptController do
88
end
99

1010
@articles %{
11+
"lifecycle" => %{title: "React Lifecycle"},
1112
"testing" => %{title: "Testing React"},
1213
"forms" => %{title: "Creating Forms in React"},
1314
"state-levels" => %{title: "Levels of State in React"},

lib/components_guide_web/templates/react_typescript/_nav.html.heex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<ul class="text-xl" data-links="block p-2 current-page-bold">
44
<li><%= link(@conn, "Fundamentals", to: "/react+typescript") %></li>
55
<li><%= link(@conn, "Testing", to: "/react+typescript/testing") %></li>
6+
<li><%= link(@conn, "Lifecycle", to: "/react+typescript/lifecycle") %></li>
67
<li><%= link(@conn, "Reducer Patterns", to: "/react+typescript/reducer-patterns") %></li>
78
<li><%= link(@conn, "Form Reducers", to: "/react+typescript/form-reducers") %></li>
89
<li><%= link(@conn, "Zero Hook Dependencies", to: "/react+typescript/zero-hook-dependencies") %></li>

lib/components_guide_web/templates/react_typescript/hooks-concurrent-world.html.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Understanding React Hooks in a Concurrent World
22

33
- `useId`: generate a unique value consistently on the server & browser side, usually for `id` attributes.
4+
- `useState` & `useReducer`: holds data that can be changed @ any time.
5+
- `useSyncExternalStore`: subscribes to an external source of data.
46
- `useMemo` & `useCallback`: recalculate output when inputs change @ render time.
5-
- `useDeferredValue`: recalculate output to be input when any urgent updates have been committed.
6-
- `useState`: recalculate output when called @ render time.
7-
- `useReducer`: recalculate output when called @ any time.
7+
- `useContext`: subscribes to data provided by another component further up the tree.
8+
- `useDeferredValue`: forks a piece of state, rendering in the background until suspended components have loaded, rendering previous state in the meanwhile.
89
- `useEffect`: executed by React when inputs change @ commit time.
9-
- `useContext`: state external to this component provided by another component further up the tree.
10-
- `useRef`: mutable reference that can be changed @ any time.
10+
- `useRef`: holds unsafe data that can be changed @ any time.
1111
- [View Official Hooks Reference](https://reactjs.org/docs/hooks-reference.html)
1212

1313
## Parallel-Universe-Safe
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# React Lifecycle
2+
3+
1. Initial render.
4+
2. Initial DOM is created.
5+
3. Browser performs layout and repainting.
6+
4. External event occurs (e.g. user interaction, promise completes).
7+
5. State changes.
8+
6. Component subtree is rendered.
9+
7. UI differences are applied to DOM.
10+
8. Browser performs layout and repainting.
11+
12+
## Client-only
13+
14+
1. Initial render:
15+
1. Calls root component (e.g. `App`) to get its returned elements.
16+
2. Iterates through elements and calls their components, and so on, until we have all leaf elements that represent HTML elements.
17+
3. Creates DOM elements for each leaf React element, and inserts them on the page.
18+
4. Runs `useLayoutEffect` callbacks.
19+
5. Browser performs layout and repainting.
20+
6. Runs `useEffect` callbacks.
21+
2. A component within the tree changes state:
22+
1. Schedules update.
23+
2. Update starts.
24+
3. Calls the changed component to get its returned elements.
25+
4. Iterates through elements and calls their components, and so on, until we have all leaf elements that represent HTML elements.
26+
5. Compares the new leaf elements to the previously committed leaf elements.
27+
6. Updates/replaces/removes DOM elements for each leaf React element.
28+
7. Runs `useLayoutEffect` callbacks.
29+
8. Browser performs layout and repainting.
30+
9. Runs `useEffect` callbacks.
31+
32+
## Not included (yet?)
33+
34+
- Hydration from server to client rendering
35+
- Suspense
36+
- `useDeferredValue`
37+
- React Server Components
38+
- Server streaming
39+
40+
## Next.js
41+
42+
## Example
43+
44+
```jsx
45+
46+
```

0 commit comments

Comments
 (0)