Skip to content

Fix useOptimistic example #7792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/content/reference/react/useOptimistic.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,40 @@ For example, when a user types a message into the form and hits the "Send" butto


```js src/App.js
import { useOptimistic, useState, useRef } from "react";
import { useOptimistic, useState, useRef, startTransition } from "react";
import { deliverMessage } from "./actions.js";

function Thread({ messages, sendMessage }) {
function Thread({ messages, sendMessageAction }) {
const formRef = useRef();
async function formAction(formData) {
function formAction(formData) {
addOptimisticMessage(formData.get("message"));
formRef.current.reset();
await sendMessage(formData);
sendMessageAction(formData);
}
const [optimisticMessages, addOptimisticMessage] = useOptimistic(
messages,
(state, newMessage) => [
...state,
{
text: newMessage,
sending: true
}
},
...state,
]
);

return (
<>
<form action={formAction} ref={formRef}>
<input type="text" name="message" placeholder="Hello!" />
<button type="submit">Send</button>
</form>
{optimisticMessages.map((message, index) => (
<div key={index}>
{message.text}
{!!message.sending && <small> (Sending...)</small>}
</div>
))}
<form action={formAction} ref={formRef}>
<input type="text" name="message" placeholder="Hello!" />
<button type="submit">Send</button>
</form>

</>
);
}
Expand All @@ -107,11 +108,15 @@ export default function App() {
const [messages, setMessages] = useState([
{ text: "Hello there!", sending: false, key: 1 }
]);
async function sendMessage(formData) {
const sentMessage = await deliverMessage(formData.get("message"));
setMessages((messages) => [...messages, { text: sentMessage }]);
function sendMessageAction(formData) {
startTransition(async () => {
const sentMessage = await deliverMessage(formData.get("message"));
startTransition(() => {
setMessages((messages) => [{ text: sentMessage }, ...messages]);
})
})
}
return <Thread messages={messages} sendMessage={sendMessage} />;
return <Thread messages={messages} sendMessageAction={sendMessageAction} />;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't really use the Action suffix convention if you do it this way because that implies that Thread wraps it in startTransition for you (which it could if you want to). That's what an Action is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
```

Expand Down