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: src/content/blog/2022/03/29/react-v18.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -240,7 +240,7 @@ With Strict Mode in React 18, React will simulate unmounting and remounting the
240
240
241
241
#### useTransition {/*usetransition*/}
242
242
243
-
`useTransition` and `startTransition` let you mark some state updates as not urgent. Other state updates are considered urgent by default. React will allow urgent state updates (for example, updating a text input) to interrupt non-urgent state updates (for example, rendering a list of search results). [See docs here](/reference/react/useTransition)
243
+
`useTransition` and `startTransition` let you mark some state updates as not urgent. Other state updates are considered urgent by default. React will allow urgent state updates (for example, updating a text input) to interrupt non-urgent state updates (for example, rendering a list of search results). [See docs here](/reference/react/useTransition).
Copy file name to clipboardexpand all lines: src/content/blog/2024/04/25/react-19.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -112,7 +112,7 @@ The async transition will immediately set the `isPending` state to true, make th
112
112
Actions automatically manage submitting data for you:
113
113
114
114
-**Pending state**: Actions provide a pending state that starts at the beginning of a request and automatically resets when the final state update is committed.
115
-
-**Optimistic updates**: Actions support the new [`useOptimistic`](#new-feature-optimistic-updates) hook so you can show users instant feedback while the requests are submitting.
115
+
-**Optimistic updates**: Actions support the new [`useOptimistic`](#new-hook-optimistic-updates) hook so you can show users instant feedback while the requests are submitting.
116
116
-**Error handling**: Actions provide error handling so you can display Error Boundaries when a request fails, and revert optimistic updates to their original value automatically.
117
117
-**Forms**: `<form>` elements now support passing functions to the `action` and `formAction` props. Passing functions to the `action` props use Actions by default and reset the form automatically after submission.
Copy file name to clipboardexpand all lines: src/content/learn/synchronizing-with-effects.md
+28-1
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,7 @@ Here and later in this text, capitalized "Effect" refers to the React-specific d
45
45
46
46
To write an Effect, follow these three steps:
47
47
48
-
1.**Declare an Effect.** By default, your Effect will run after every render.
48
+
1.**Declare an Effect.** By default, your Effect will run after every [commit](/learn/render-and-commit).
49
49
2.**Specify the Effect dependencies.** Most Effects should only re-run *when needed* rather than after every render. For example, a fade-in animation should only trigger when a component appears. Connecting and disconnecting to a chat room should only happen when the component appears and disappears, or when the chat room changes. You will learn how to control this by specifying *dependencies.*
50
50
3.**Add cleanup if needed.** Some Effects need to specify how to stop, undo, or clean up whatever they were doing. For example, "connect" needs "disconnect", "subscribe" needs "unsubscribe", and "fetch" needs either "cancel" or "ignore". You will learn how to do this by returning a *cleanup function*.
51
51
@@ -598,6 +598,33 @@ Usually, the answer is to implement the cleanup function. The cleanup function
598
598
599
599
Most of the Effects you'll write will fit into one of the common patterns below.
600
600
601
+
<Pitfall>
602
+
603
+
#### Don't use refs to prevent Effects from firing {/*dont-use-refs-to-prevent-effects-from-firing*/}
604
+
605
+
A common pitfall for preventing Effects firing twice in development is to use a `ref` to prevent the Effect from running more than once. For example, you could "fix" the above bug with a `useRef`:
606
+
607
+
```js {1,3-4}
608
+
constconnectionRef=useRef(null);
609
+
useEffect(() => {
610
+
// 🚩 This wont fix the bug!!!
611
+
if (!connectionRef.current) {
612
+
connectionRef.current=createConnection();
613
+
connectionRef.current.connect();
614
+
}
615
+
}, []);
616
+
```
617
+
618
+
This makes it so you only see `"✅ Connecting..."` once in development, but it doesn't fix the bug.
619
+
620
+
When the user navigates away, the connection still isn't closed and when they navigate back, a new connection is created. As the user navigates across the app, the connections would keep piling up, the same as it would before the "fix".
621
+
622
+
To fix the bug, it is not enough to just make the Effect run once. The effect needs to work after re-mounting, which means the connection needs to be cleaned up like in the solution above.
623
+
624
+
See the examples below for how to handle common patterns.
Sometimes you need to add UI widgets that aren't written to React. For example, let's say you're adding a map component to your page. It has a `setZoomLevel()` method, and you'd like to keep the zoom level in sync with a `zoomLevel` state variable in your React code. Your Effect would look similar to this:
Copy file name to clipboardexpand all lines: src/content/learn/tutorial-tic-tac-toe.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -2915,4 +2915,4 @@ If you have extra time or want to practice your new React skills, here are some
2915
2915
1. When someone wins, highlight the three squares that caused the win (and when no one wins, display a message about the result being a draw).
2916
2916
1. Display the location for each move in the format (row, col) in the move history list.
2917
2917
2918
-
Throughout this tutorial, you've touched on React concepts including elements, components, props, and state. Now that you've seen how these concepts work when building a game, check out [Thinking in React](/learn/thinking-in-react) to see how the same React concepts work when build an app's UI.
2918
+
Throughout this tutorial, you've touched on React concepts including elements, components, props, and state. Now that you've seen how these concepts work when building a game, check out [Thinking in React](/learn/thinking-in-react) to see how the same React concepts work when building an app's UI.
0 commit comments