Skip to content

Commit df9c721

Browse files
committed
Improve react fundamentals article
1 parent 511be52 commit df9c721

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Understanding React Hooks in a Concurrent World
22

3-
- `useId`: generate a unique value consistently on the server & browser side.
3+
- `useId`: generate a unique value consistently on the server & browser side, usually for `id` attributes.
44
- `useMemo` & `useCallback`: recalculate output when inputs change @ render time.
55
- `useDeferredValue`: recalculate output to be input when any urgent updates have been committed.
66
- `useState`: recalculate output when called @ render time.

lib/components_guide_web/templates/react_typescript/tips.html.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ You can see the single responsibility being enforced here — there’s a single
150150
Functions should ideally be deterministic and referentially transparent. Woah — what are these words? In English:
151151

152152
- _Deterministic:_ always produce the same result given the same input.
153-
- _Referentially transparent:_ if you can replace the usage of a function with a hard-coded value it produces the same behaviour.
153+
- _Referentially transparent:_ when you can replace the usage of a function with a hard-coded value without changing the behaviour.
154154

155155
To understand, it might be worth asking what if our functions didn’t have these properties:
156156

157-
- If we couldn’t *determine* all the inputs that affected how a function worked, it would be hard to understand and debug. Think of global variables, or transient state.
158-
- If we couldn’t just paste in a hard coded value instead of calling a function, it would make caching and mocking difficult.
157+
- If we couldn’t *determine* all the inputs that affected how a function worked, it would be hard to understand and debug. Think of global variables or deeply nested state.
158+
- If we couldn’t just paste in a hard coded value instead of using the result of calling a function, it would make caching and mocking those results difficult.
159159

160160
## What is a React component?
161161

162-
With these concepts down, it’s worth asking what is a React component?
162+
With these concepts down, it’s worth asking: _what is a React component?_
163163

164164
**A React component is a deterministic and referentially transparent function that takes in props as input, and produce changes to the DOM as output.**
165165

@@ -170,9 +170,10 @@ The general life-cycle of the React engine is:
170170
3. Gather all the leaf HTML elements that all the components produced.
171171
4. Find the differences since the last render, and build a list of DOM changes to be made.
172172
5. Actually commit the changes to the DOM.
173-
6. Call `useLayoutEffect` hooks.
174-
7. Allow the browser to paint and show the user the changes.
175-
8. Call `useEffect` hooks.
173+
6. Register event handlers in a private object owned by React.
174+
7. Call `useLayoutEffect` hooks.
175+
8. Allow the browser to paint and show the user the changes.
176+
9. Call `useEffect` hooks.
176177

177178
There are a few things to note here. The DOM isn’t actually changed until step 5! Our components are merely instructions to the React engine.
178179

@@ -231,11 +232,13 @@ A React hook lets component authors perform more advanced things outside the pur
231232

232233
- `useState` — store data that the component relies on for rendering.
233234
- `useReducer` — more flexible version of `useState`.
235+
- `useId`: generate a unique value consistently on the server & browser side, usually for `id` attributes.
234236
- `useEffect` — perform side effect like fetching data or storing in local storage.
235237
- `useLayoutEffect` — perform change to the DOM like focus.
236238
- `useRef` — store data that the component relies on for effects or event handlers.
237239
- `useContext` — use state provided by a higher up component.
238240
- `useMemo` — perform expensive calculations that would be the same across multiple renders.
241+
- `useSyncExternalStore`: read changing data from an external store into your component.
239242

240243
## Prefer composition
241244

0 commit comments

Comments
 (0)