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: lib/components_guide_web/templates/react_typescript/tips.html.md
+10-7Lines changed: 10 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,16 +150,16 @@ You can see the single responsibility being enforced here — there’s a single
150
150
Functions should ideally be deterministic and referentially transparent. Woah — what are these words? In English:
151
151
152
152
-_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.
154
154
155
155
To understand, it might be worth asking what if our functions didn’t have these properties:
156
156
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.
159
159
160
160
## What is a React component?
161
161
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?_
163
163
164
164
**A React component is a deterministic and referentially transparent function that takes in props as input, and produce changes to the DOM as output.**
165
165
@@ -170,9 +170,10 @@ The general life-cycle of the React engine is:
170
170
3. Gather all the leaf HTML elements that all the components produced.
171
171
4. Find the differences since the last render, and build a list of DOM changes to be made.
172
172
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.
176
177
177
178
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.
178
179
@@ -231,11 +232,13 @@ A React hook lets component authors perform more advanced things outside the pur
231
232
232
233
-`useState` — store data that the component relies on for rendering.
233
234
-`useReducer` — more flexible version of `useState`.
235
+
-`useId`: generate a unique value consistently on the server & browser side, usually for `id` attributes.
234
236
-`useEffect` — perform side effect like fetching data or storing in local storage.
235
237
-`useLayoutEffect` — perform change to the DOM like focus.
236
238
-`useRef` — store data that the component relies on for effects or event handlers.
237
239
-`useContext` — use state provided by a higher up component.
238
240
-`useMemo` — perform expensive calculations that would be the same across multiple renders.
241
+
-`useSyncExternalStore`: read changing data from an external store into your component.
0 commit comments