Skip to content

Commit 46b45fb

Browse files
Add useOptimistic Hook API reference documentation (#6377)
* Add useOptimistic API reference documentation * Fix title * Add working exmaple from form and updadd parameters * Add updates to useOptimistic * Add updates to useOptimistic * Add updates to useOptimistic --------- Co-authored-by: Matt Carroll <mattca@meta.com>
1 parent 5d2113b commit 46b45fb

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
title: useOptimistic
3+
canary: true
4+
---
5+
6+
<Canary>
7+
8+
The `useOptimistic` Hook is currently only available in React's canary and experimental channels. Learn more about [React's release channels here](/community/versioning-policy#all-release-channels).
9+
10+
</Canary>
11+
12+
<Intro>
13+
14+
`useOptimistic` is a React Hook that lets you optimistically update the UI.
15+
16+
```js
17+
const [optimisticState, addOptimistic] = useOptimistic(state, updateFn);
18+
```
19+
20+
</Intro>
21+
22+
<InlineToc />
23+
24+
---
25+
26+
## Reference {/*reference*/}
27+
28+
### `useOptimistic(state, updateFn)` {/*use*/}
29+
30+
`useOptimistic` is a React hook that lets you show a different state while an async action is underway. It accepts some state as an argument and returns a copy of that state that can be different during the duration of an async action such as a network request. You provide a function that takes the current state and the input to the action, and returns the optimistic state to be used while the action is pending.
31+
32+
This state is called the "optimistic" state because it is usually used to immediately present the user with the result of performing an action, even though the action actually takes time to complete.
33+
34+
```js
35+
import { useOptimistic } from 'react';
36+
37+
function AppContainer() {
38+
const [optimisticState, addOptimistic] = useOptimistic(
39+
state,
40+
// updateFn
41+
(currentState, optimisticValue) => {
42+
// merge and return new state
43+
// with optimistic value
44+
}
45+
);
46+
}
47+
```
48+
49+
[See more examples below.](#usage)
50+
51+
#### Parameters {/*parameters*/}
52+
53+
* `state`: the value to be returned initially and whenever no action is pending.
54+
* `updateFn(currentState, optimisticValue)`: a function that takes the current state and the optimistic value passed to `addOptimistic` and returns the resulting optimistic state. It must be a pure function. `updateFn` takes in two parameters. The `currentState` and the `optimisticValue`. The return value will be the merged value of the `currentState` and `optimisticValue`.
55+
56+
57+
#### Returns {/*returns*/}
58+
59+
* `optimisticState`: The resulting optimistic state. It is equal to `state` unless an action is pending, in which case it is equal to the value returned by `updateFn`.
60+
* `addOptimistic`: `addOptimistic` is the dispatching function to call when you have an optimistic update. It takes one argument, `optimisticValue`, of any type and will call the `updateFn` with `state` and `optimisticValue`.
61+
62+
---
63+
64+
## Usage {/*usage*/}
65+
66+
### Optimistically updating forms {/*optimistically-updating-with-forms*/}
67+
68+
The `useOptimistic` Hook provides a way to optimistically update the user interface before a background operation, like a network request, completes. In the context of forms, this technique helps to make apps feel more responsive. When a user submits a form, instead of waiting for the server's response to reflect the changes, the interface is immediately updated with the expected outcome.
69+
70+
For example, when a user types a message into the form and hits the "Send" button, the `useOptimistic` Hook allows the message to immediately appear in the list with a "Sending..." label, even before the message is actually sent to a server. This "optimistic" approach gives the impression of speed and responsiveness. The form then attempts to truly send the message in the background. Once the server confirms the message has been received, the "Sending..." label is removed.
71+
72+
<Sandpack>
73+
74+
75+
```js App.js
76+
import { useOptimistic, useState, useRef } from "react";
77+
import { deliverMessage } from "./actions.js";
78+
79+
function Thread({ messages, sendMessage }) {
80+
const formRef = useRef();
81+
async function formAction(formData) {
82+
addOptimisticMessage(formData.get("message"));
83+
formRef.current.reset();
84+
await sendMessage(formData);
85+
}
86+
const [optimisticMessages, addOptimisticMessage] = useOptimistic(
87+
messages,
88+
(state, newMessage) => [
89+
...state,
90+
{
91+
text: newMessage,
92+
sending: true
93+
}
94+
]
95+
);
96+
97+
return (
98+
<>
99+
{optimisticMessages.map((message, index) => (
100+
<div key={index}>
101+
{message.text}
102+
{!!message.sending && <small> (Sending...)</small>}
103+
</div>
104+
))}
105+
<form action={formAction} ref={formRef}>
106+
<input type="text" name="message" placeholder="Hello!" />
107+
<button type="submit">Send</button>
108+
</form>
109+
</>
110+
);
111+
}
112+
113+
export default function App() {
114+
const [messages, setMessages] = useState([
115+
{ text: "Hello there!", sending: false, key: 1 }
116+
]);
117+
async function sendMessage(formData) {
118+
const sentMessage = await deliverMessage(formData.get("message"));
119+
setMessages([...messages, { text: sentMessage }]);
120+
}
121+
return <Thread messages={messages} sendMessage={sendMessage} />;
122+
}
123+
```
124+
125+
```js actions.js
126+
export async function deliverMessage(message) {
127+
await new Promise((res) => setTimeout(res, 1000));
128+
return message;
129+
}
130+
```
131+
132+
133+
```json package.json hidden
134+
{
135+
"dependencies": {
136+
"react": "18.3.0-canary-6db7f4209-20231021",
137+
"react-dom": "18.3.0-canary-6db7f4209-20231021",
138+
"react-scripts": "^5.0.0"
139+
},
140+
"main": "/index.js",
141+
"devDependencies": {}
142+
}
143+
```
144+
145+
</Sandpack>

src/sidebarReference.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
"title": "useMemo",
5656
"path": "/reference/react/useMemo"
5757
},
58+
{
59+
"title": "useOptimistic",
60+
"path": "/reference/react/useOptimistic",
61+
"canary": true
62+
},
5863
{
5964
"title": "useReducer",
6065
"path": "/reference/react/useReducer"

0 commit comments

Comments
 (0)