Skip to content

Commit 3611495

Browse files
authored
Add Strict Effects to Strict Mode (#4362)
* Add Strict Effects to Strict Mode * Update with new thinking
1 parent ac0316d commit 3611495

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

content/docs/strict-mode.md

+49
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ In the above example, strict mode checks will *not* be run against the `Header`
2121
* [Warning about deprecated findDOMNode usage](#warning-about-deprecated-finddomnode-usage)
2222
* [Detecting unexpected side effects](#detecting-unexpected-side-effects)
2323
* [Detecting legacy context API](#detecting-legacy-context-api)
24+
* [Detecting unsafe effects](#detecting-unsafe-effects)
2425

2526
Additional functionality will be added with future releases of React.
2627

@@ -127,3 +128,51 @@ The legacy context API is error-prone, and will be removed in a future major ver
127128
![](../images/blog/warn-legacy-context-in-strict-mode.png)
128129

129130
Read the [new context API documentation](/docs/context.html) to help migrate to the new version.
131+
132+
133+
### Detecting unsafe effects {#detecting-unsafe-effects}
134+
135+
In the future, we’d like to add a feature that allows React to add and remove sections of the UI while preserving state. For example, when a user tabs away from a screen and back, React should be able to immediately show the previous screen. To do this, React support remounting trees using the same component state used before unmounting.
136+
137+
This feature will give React better performance out-of-the-box, but requires components to be resilient to effects being mounted and destroyed multiple times. Most effects will work without any changes, but some effects do not properly clean up subscriptions in the destroy callback, or implicitly assume they are only mounted or destroyed once.
138+
139+
To help surface these issues, React 18 introduces a new development-only check to Strict Mode. This new check will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount.
140+
141+
To demonstrate the development behavior you'll see in Strict Mode with this feature, consider what happens when React mounts a new component. Without this change, when a component mounts, React creates the effects:
142+
143+
```
144+
* React mounts the component.
145+
* Layout effects are created.
146+
* Effects are created.
147+
```
148+
149+
With Strict Mode starting in React 18, whenever a component mounts in development, React will simulate immediately unmounting and remounting the component:
150+
151+
```
152+
* React mounts the component.
153+
* Layout effects are created.
154+
* Effect effects are created.
155+
* React simulates effects being destroyed on a mounted component.
156+
* Layout effects are destroyed.
157+
* Effects are destroyed.
158+
* React simulates effects being re-created on a mounted component.
159+
* Layout effects are created
160+
* Effect setup code runs
161+
```
162+
163+
On the second mount, React will restore the state from the first mount. This feature simulates user behavior such as a user tabbing away from a screen and back, ensuring that code will properly handle state restoration.
164+
165+
When the component unmounts, effects are destroyed as normal:
166+
167+
```
168+
* React unmounts the component.
169+
* Layout effects are destroyed.
170+
* Effect effects are destroyed.
171+
```
172+
173+
> Note:
174+
>
175+
> This only applies to development mode, _production behavior is unchanged_.
176+
177+
For help supporting common issues, see:
178+
- [How to Support Strict Effects](https://github.com/reactwg/react-18/discussions/18)

0 commit comments

Comments
 (0)