About React @ Sun, Feb 12, 2023 7:33 PM
About React @ Sun, Feb 12, 2023 8:02 PM
Expand 11 lines ...
12

12

13
+ [Update to allow components to render undefined](https://github.com/reactwg/react-18/discussions/75)
13
+ [Update to allow components to render undefined](https://github.com/reactwg/react-18/discussions/75)
14
+ [Automatic batching for fewer renders in React 18](https://github.com/reactwg/react-18/discussions/21)
14
+ [Automatic batching for fewer renders in React 18](https://github.com/reactwg/react-18/discussions/21)
15
-
+ 
15
+
+ [New in 18: useEffect fires synchronously when it's the result of a discrete input](https://github.com/reactwg/react-18/discussions/128)
16

16

17

17

18
## groups multiple state updates into a single re-render
18
## groups multiple state updates into a single re-render
Expand 78 lines ...
97

97

98
I am not sure about the 5ms timing, but batching isn't related to queuing within 5ms as far as I understand, batching makes sure that app isn't rendered until it executes the whole callback. But If we use the concurrent api `startTransition` instead of `setTimeout` then the `startTransition` will be executed immediately and the updates inside it will be marked as transition which could be interrupted incase of some urgent update (click) kicking in.
98
I am not sure about the 5ms timing, but batching isn't related to queuing within 5ms as far as I understand, batching makes sure that app isn't rendered until it executes the whole callback. But If we use the concurrent api `startTransition` instead of `setTimeout` then the `startTransition` will be executed immediately and the updates inside it will be marked as transition which could be interrupted incase of some urgent update (click) kicking in.
99
+

100
+

101
+
## discrete input
102
+

103
+
In React 17 and below, the function passed to useEffect typically fires asynchronously after the browser has painted. The idea is to defer as much work as possible until paint so that the user experience is not delayed. This includes things like setting up subscriptions.
104
+

105
+
For example, imagine you're building a form that disables "submit" after the first submission:
106
+
```js
107
+
useEffect(() => {
108
+
  if (!disableSubmit) {
109
+
    const form = formRef.current;
110
+
    form.addEventListener('submit', onSubmit);
111
+
    return () => {
112
+
      form.removeEventListener('submit', onSubmit);
113
+
    };
114
+
  }
115
+
}, [disableSubmit, onSubmit]);
116
+
```
117
+
If the user attempts to submit the form multiple times in quick succession, we need to make sure the effects from the first event have completely finished before the next input is processed. So we synchronously flush them.
118
+

119
+
We don't need to do this for events that aren't discrete, because we assume they are not order-dependent and do not need to be observed by external systems.
120
+

121
+
似乎其实用 useLayoutEffect 也就行了,但是 useLayoutEffect 会让其导致的更新也会同步执行。  
122
+
Note that this only affects the timing of when useEffect functions are called. **It does not affect the priority of updates that are triggered inside a useEffect**. They still get the default priority. (This is in contrast to useLayoutEffect, which not only calls the effect callback synchronously but also gives synchronous priority to its updates.)