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
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