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
You can think of Effect Events as being very similar to event handlers. The main difference is that event handlers run in response to user interactions, whereas Effect Events are triggered by you from Effects. Effect Events let you "break the chain" between the reactivity of Effects and code that should not be reactive.
575
-
>>>>>>> 38b52cfdf059b2efc5ee3223a758efe00319fcc7
576
572
577
573
### 使用 Effect Event 读取最新的 props 和 state {/*reading-latest-props-and-state-with-effect-events*/}
`useOptimistic` 是一个 React Hook,它允许你在进行异步操作时显示不同 state。它接受 state 作为参数,并返回该 state 的副本,在异步操作(如网络请求)期间可以不同。你需要提供一个函数,该函数接受当前 state 和操作的输入,并返回在操作挂起期间要使用的乐观状态。
* `value`: The value returned when there are no pending Actions.
40
+
* `value`: 当没有待处理的操作时返回的值。
64
41
* **optional** `reducer(currentState, action)`: The reducer function that specifies how the optimistic state gets updated. It must be pure, should take the current state and reducer action arguments, and should return the next optimistic state.
`useOptimistic` returns an array with exactly two values:
74
46
75
47
1. `optimisticState`: The current optimistic state. It is equal to `value` unless an Action is pending, in which case it is equal to the state returned by `reducer` (or the value passed to the set function if no `reducer` was provided).
@@ -167,19 +139,11 @@ The `value` argument to `useOptimistic` determines what displays after the Actio
167
139
If the Action throws an error, the Transition still ends, and React renders with whatever `value` currently is. Since the parent typically only updates `value` on success, a failure means `value` hasn't changed, so the UI shows what it showed before the optimistic update. You can catch the error to show a message to the user.
### Adding optimistic state to a component {/*adding-optimistic-state-to-a-component*/}
184
148
185
149
Call `useOptimistic` at the top level of your component to declare one or more optimistic states.
@@ -242,7 +206,6 @@ For an example, see: [Using optimistic state in Action props](#using-optimistic-
242
206
In an [Action prop](/reference/react/useTransition#exposing-action-props-from-components), you can call the optimistic setter directly without `startTransition`.
243
207
244
208
This example sets optimistic state inside a `<form>``submitAction` prop:
0 commit comments