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
This is because we're fully unmounting `ContactTab` in `App`. When the Contact tab unmounts, the `<textarea>` element's internal DOM state is lost.
439
+
This is because we're fully unmounting `Contact` in `App`. When the Contact tab unmounts, the `<textarea>` element's internal DOM state is lost.
440
440
441
441
If we switch to using an Activity boundary to show and hide the active tab, we can preserve the state of each tab's DOM. Try entering text and switching tabs again, and you'll see the draft message is no longer reset:
442
442
@@ -445,8 +445,8 @@ If we switch to using an Activity boundary to show and hide the active tab, we c
@@ -915,7 +915,7 @@ This feature is called Selective Hydration, and it's an under-the-hood optimizat
915
915
916
916
## Troubleshooting {/*troubleshooting*/}
917
917
918
-
### Preventing hidden content from having unwanted side effects {/*preventing-hidden-content-from-having-unwanted-side-effects*/}
918
+
### My hidden components have unwanted side effects {/*my-hidden-components-have-unwanted-side-effects*/}
919
919
920
920
An Activity boundary hides its content by setting `display: none` on its children and cleaning up any of their [Effects](/reference/react/useEffect). So, most well-behaved React components that properly clean up their side effects will already be robust to being hidden by Activity.
921
921
@@ -928,8 +928,8 @@ As an example, consider a `<video>` tag. Typically it doesn't require any cleanu
@@ -1139,7 +1139,7 @@ To fix this, we can add an Effect with a cleanup function that pauses the video:
1139
1139
exportdefaultfunctionVideoTab() {
1140
1140
constref=useRef();
1141
1141
1142
-
useEffect(() => {
1142
+
useLayoutEffect(() => {
1143
1143
constvideoRef=ref.current;
1144
1144
1145
1145
return () => {
@@ -1159,15 +1159,17 @@ export default function VideoTab() {
1159
1159
}
1160
1160
```
1161
1161
1162
+
We call `useLayoutEffect` instead of `useEffect` because conceptually the clean-up code is tied to the component's UI being visually hidden. If we used a regular effect, the code could be delayed by (say) a re-suspending Suspense boundary or a View Transition.
@@ -1297,9 +1299,9 @@ To eagerly discover other Effects that don't have proper cleanup, which is impor
1297
1299
---
1298
1300
1299
1301
1300
-
### Effects don't mount when an Activity is hidden {/*effects-dont-mount-when-an-activity-is-hidden*/}
1302
+
### My hidden components have Effects that aren't running {/*my-hidden-components-have-effects-that-arent-running*/}
1301
1303
1302
-
When an `<Activity>` is "hidden", all Effects are cleaned up. Conceptually, the children are unmounted, but React saves their state for later. This is a feature of Activity because it means subscriptions won't be active for hidden parts of the UI, reducing the amount of work needed for hidden content.
1304
+
When an `<Activity>` is "hidden", all its children's Effects are cleaned up. Conceptually, the children are unmounted, but React saves their state for later. This is a feature of Activity because it means subscriptions won't be active for hidden parts of the UI, reducing the amount of work needed for hidden content.
1303
1305
1304
1306
If you're relying on an Effect mounting to clean up a component's side effects, refactor the Effect to do the work in the returned cleanup function instead.
0 commit comments