Skip to content

Commit 69a6525

Browse files
authored
docs(en): merge reactjs.org/main into zh-hans.reactjs.org/main @ d6c4c0f (#1711)
2 parents f2101ab + 05a231d commit 69a6525

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/content/blog/2025/04/23/react-labs-view-transitions-activity-and-more.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ You can try them by upgrading React packages to the most recent experimental ver
5151

5252
Read on to learn how to use these features in your app, or check out the newly published docs:
5353

54-
- [`<ViewTransition>`](/reference/react/ViewTransition): A component lets you activate an animation for a Transition.
54+
- [`<ViewTransition>`](/reference/react/ViewTransition): A component that lets you activate an animation for a Transition.
5555
- [`addTransitionType`](/reference/react/addTransitionType): A function that allows you to specify the cause of a Transition.
56-
- [`<Activity>`](/reference/react/Activity): A component that lets you hide and show part of the UI.
56+
- [`<Activity>`](/reference/react/Activity): A component that lets you hide and show parts of the UI.
5757

5858
## View Transitions {/*view-transitions*/}
5959

@@ -14355,4 +14355,4 @@ This research is still early. We'll share more, and what the new APIs will look
1435514355

1435614356
---
1435714357

14358-
_Thanks to [Aurora Scharff](https://bsky.app/profile/aurorascharff.no), [Dan Abramov](https://bsky.app/profile/danabra.mov), [Eli White](https://twitter.com/Eli_White), [Lauren Tan](https://bsky.app/profile/no.lol), [Luna Wei](https://github.com/lunaleaps), [Matt Carroll](https://twitter.com/mattcarrollcode), [Jack Pope](https://jackpope.me), [Jason Bonta](https://threads.net/someextent), [Jordan Brown](https://github.com/jbrown215), [Jordan Eldredge](https://bsky.app/profile/capt.dev), [Mofei Zhang](https://threads.net/z_mofei), [Sebastien Lorber](https://bsky.app/profile/sebastienlorber.com), [Sebastian Markbåge](https://bsky.app/profile/sebmarkbage.calyptus.eu), and [Tim Yung](https://github.com/yungsters) for reviewing this post._
14358+
_Thanks to [Aurora Scharff](https://bsky.app/profile/aurorascharff.no), [Dan Abramov](https://bsky.app/profile/danabra.mov), [Eli White](https://twitter.com/Eli_White), [Lauren Tan](https://bsky.app/profile/no.lol), [Luna Wei](https://github.com/lunaleaps), [Matt Carroll](https://twitter.com/mattcarrollcode), [Jack Pope](https://jackpope.me), [Jason Bonta](https://threads.net/someextent), [Jordan Brown](https://github.com/jbrown215), [Jordan Eldredge](https://bsky.app/profile/capt.dev), [Mofei Zhang](https://threads.net/z_mofei), [Sebastien Lorber](https://bsky.app/profile/sebastienlorber.com), [Sebastian Markbåge](https://bsky.app/profile/sebmarkbage.calyptus.eu), and [Tim Yung](https://github.com/yungsters) for reviewing this post._

src/content/reference/react/useOptimistic.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,39 +66,40 @@ function AppContainer() {
6666

6767

6868
```js src/App.js
69-
import { useOptimistic, useState, useRef } from "react";
69+
import { useOptimistic, useState, useRef, startTransition } from "react";
7070
import { deliverMessage } from "./actions.js";
7171

72-
function Thread({ messages, sendMessage }) {
72+
function Thread({ messages, sendMessageAction }) {
7373
const formRef = useRef();
74-
async function formAction(formData) {
74+
function formAction(formData) {
7575
addOptimisticMessage(formData.get("message"));
7676
formRef.current.reset();
77-
await sendMessage(formData);
77+
sendMessageAction(formData);
7878
}
7979
const [optimisticMessages, addOptimisticMessage] = useOptimistic(
8080
messages,
8181
(state, newMessage) => [
82-
...state,
8382
{
8483
text: newMessage,
8584
sending: true
86-
}
85+
},
86+
...state,
8787
]
8888
);
8989

9090
return (
9191
<>
92+
<form action={formAction} ref={formRef}>
93+
<input type="text" name="message" placeholder="你好!" />
94+
<button type="submit">发送</button>
95+
</form>
9296
{optimisticMessages.map((message, index) => (
9397
<div key={index}>
9498
{message.text}
9599
{!!message.sending && <small>(发送中……)</small>}
96100
</div>
97101
))}
98-
<form action={formAction} ref={formRef}>
99-
<input type="text" name="message" placeholder="你好!" />
100-
<button type="submit">发送</button>
101-
</form>
102+
102103
</>
103104
);
104105
}
@@ -107,11 +108,15 @@ export default function App() {
107108
const [messages, setMessages] = useState([
108109
{ text: "你好,在这儿!", sending: false, key: 1 }
109110
]);
110-
async function sendMessage(formData) {
111-
const sentMessage = await deliverMessage(formData.get("message"));
112-
setMessages((messages) => [...messages, { text: sentMessage }]);
111+
function sendMessageAction(formData) {
112+
startTransition(async () => {
113+
const sentMessage = await deliverMessage(formData.get("message"));
114+
startTransition(() => {
115+
setMessages((messages) => [{ text: sentMessage }, ...messages]);
116+
})
117+
})
113118
}
114-
return <Thread messages={messages} sendMessage={sendMessage} />;
119+
return <Thread messages={messages} sendMessageAction={sendMessageAction} />;
115120
}
116121
```
117122

0 commit comments

Comments
 (0)