Implement experimental_useOptimisticState (#26740)

This adds an experimental hook tentatively called useOptimisticState.
(The actual name needs some bikeshedding.)

The headline feature is that you can use it to implement optimistic
updates. If you set some optimistic state during a transition/action,
the state will be automatically reverted once the transition completes.

Another feature is that the optimistic updates will be continually
rebased on top of the latest state.

It's easiest to explain with examples; we'll publish documentation as
the API gets closer to stabilizing. See tests for now.

Technically the use cases for this hook are broader than just optimistic
updates; you could use it implement any sort of "pending" state, such as
the ones exposed by useTransition and useFormStatus. But we expect
people will most often reach for this hook to implement the optimistic
update pattern; simpler cases are covered by those other hooks.
This commit is contained in:
Andrew Clark
2023-05-01 13:19:20 -04:00
committed by GitHub
parent 667f7e26c9
commit bae77eff8a
13 changed files with 846 additions and 66 deletions
+2 -1
View File
@@ -463,5 +463,6 @@
"475": "Internal React Error: suspendedState null when it was expected to exists. Please report this as a React bug.",
"476": "Expected the form instance to be a HostComponent. This is a bug in React.",
"477": "React Internal Error: processHintChunk is not implemented for Native-Relay. The fact that this method was called means there is a bug in React.",
"478": "Thenable should have already resolved. This is a bug in React."
"478": "Thenable should have already resolved. This is a bug in React.",
"479": "Cannot update optimistic state while rendering."
}