Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -11469,7 +11469,7 @@ root.render(

**`<Activity />` 现在可以在 React Canary 版本使用。**

[了解更多关于 React 版本发布的内容](/community/versioning-policy#all-release-channels)
[了解更多关于 React 版本发布的内容](/community/versioning-policy#all-release-channels)

</Note>

Expand Down Expand Up @@ -14303,7 +14303,7 @@ useEffect(() => {
}); // 编译器插入的依赖项。
```

使用这段代码,React 编译器可以为你推断依赖项并自动插入它们,这样你就不需要看到或编写它们。通过像[IDE 扩展](#compiler-ide-extension)和[`useEffectEvent`](/reference/react/experimental_useEffectEvent)这样的功能,我们可以提供一个 CodeLens 来显示编译器在你需要调试时插入的内容,或通过移除依赖项来优化。这有助于强化编写 Effects 的正确心智模型,即 Effects 可以在任何时候运行,以将你的组件或 hook 的状态与其他内容同步。
使用这段代码,React 编译器可以为你推断依赖项并自动插入它们,这样你就不需要看到或编写它们。通过像[IDE 扩展](#compiler-ide-extension)和[`useEffectEvent`](/reference/react/useEffectEvent)这样的功能,我们可以提供一个 CodeLens 来显示编译器在你需要调试时插入的内容,或通过移除依赖项来优化。这有助于强化编写 Effects 的正确心智模型,即 Effects 可以在任何时候运行,以将你的组件或 hook 的状态与其他内容同步。

我们希望自动插入依赖项不仅更容易编写,而且通过迫使你从 Effect 的作用角度思考,而不是从组件生命周期角度思考,使它们更容易理解。

Expand Down
6 changes: 3 additions & 3 deletions src/content/learn/escape-hatches.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ label { display: block; margin-top: 10px; }
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest",
"toastify-js": "1.12.0"
},
Expand All @@ -471,7 +471,7 @@ label { display: block; margin-top: 10px; }

```js
import { useState, useEffect } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';
import { createConnection, sendMessage } from './chat.js';
import { showNotification } from './notifications.js';

Expand Down
32 changes: 17 additions & 15 deletions src/content/learn/removing-effect-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,13 @@ function ChatRoom({ roomId }) {

### 你想读取一个值而不对其变化做出“反应”吗? {/*do-you-want-to-read-a-value-without-reacting-to-its-changes*/}

<Wip>
<Canary>

本节描述了一个在稳定版本的 React 中 **尚未发布的实验性** API。
**`useEffectEvent` API 当前仅在 React Canary 和 实验发行版中可用**。

</Wip>
[了解更多关于 React 版本发布的内容](/community/versioning-policy#all-release-channels)。

</Canary>

假设你希望在用户收到新消息时播放声音,`isMuted` 为 `true` 除外:

Expand Down Expand Up @@ -1261,8 +1263,8 @@ Effect 中是否有一行代码不应该是响应式的?如何将非响应式
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest"
},
"scripts": {
Expand All @@ -1276,7 +1278,7 @@ Effect 中是否有一行代码不应该是响应式的?如何将非响应式

```js
import { useState, useEffect, useRef } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';
import { FadeInAnimation } from './animation.js';

function Welcome({ duration }) {
Expand Down Expand Up @@ -1388,8 +1390,8 @@ Effect 需要读取 `duration` 的最新值,但你不希望它对 `duration`
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest"
},
"scripts": {
Expand All @@ -1404,7 +1406,7 @@ Effect 需要读取 `duration` 的最新值,但你不希望它对 `duration`
```js
import { useState, useEffect, useRef } from 'react';
import { FadeInAnimation } from './animation.js';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';

function Welcome({ duration }) {
const ref = useRef(null);
Expand Down Expand Up @@ -1824,8 +1826,8 @@ label, button { display: block; margin-bottom: 5px; }
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest",
"toastify-js": "1.12.0"
},
Expand Down Expand Up @@ -1906,7 +1908,7 @@ export default function App() {

```js src/ChatRoom.js active
import { useState, useEffect } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';

export default function ChatRoom({ roomId, createConnection, onMessage }) {
useEffect(() => {
Expand Down Expand Up @@ -2119,8 +2121,8 @@ export default function ChatRoom({ roomId, isEncrypted, onMessage }) { // Reacti
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest",
"toastify-js": "1.12.0"
},
Expand Down Expand Up @@ -2188,7 +2190,7 @@ export default function App() {

```js src/ChatRoom.js active
import { useState, useEffect } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';
import {
createEncryptedConnection,
createUnencryptedConnection,
Expand Down
32 changes: 17 additions & 15 deletions src/content/learn/reusing-logic-with-custom-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -837,11 +837,13 @@ export default function ChatRoom({ roomId }) {

### 把事件处理函数传到自定义 Hook 中 {/*passing-event-handlers-to-custom-hooks*/}

<Wip>
<Canary>

这个章节描述了 React 稳定版 **还未发布的一个实验性 API**。
**`useEffectEvent` API 当前仅在 React Canary 和 实验发行版中可用**。

</Wip>
[了解更多关于 React 版本发布的内容](/community/versioning-policy#all-release-channels)。

</Canary>

当你在更多组件中使用 `useChatRoom` 时,你可能希望组件能定制它的行为。例如现在 Hook 内部收到消息的处理逻辑是硬编码:

Expand Down Expand Up @@ -985,7 +987,7 @@ export default function ChatRoom({ roomId }) {

```js src/useChatRoom.js
import { useEffect } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';
import { createConnection } from './chat.js';

export function useChatRoom({ serverUrl, roomId, onReceiveMessage }) {
Expand Down Expand Up @@ -1070,8 +1072,8 @@ export function showNotification(message, theme = 'dark') {
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest",
"toastify-js": "1.12.0"
},
Expand Down Expand Up @@ -1666,7 +1668,7 @@ export default function App() {

```js src/useFadeIn.js active
import { useState, useEffect } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';

export function useFadeIn(ref, duration) {
const [isRunning, setIsRunning] = useState(true);
Expand Down Expand Up @@ -1719,8 +1721,8 @@ html, body { min-height: 300px; }
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest"
},
"scripts": {
Expand Down Expand Up @@ -2208,8 +2210,8 @@ export function useInterval(onTick, delay) {
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest"
},
"scripts": {
Expand Down Expand Up @@ -2252,7 +2254,7 @@ export function useCounter(delay) {

```js src/useInterval.js
import { useEffect } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';

export function useInterval(onTick, delay) {
useEffect(() => {
Expand All @@ -2279,8 +2281,8 @@ export function useInterval(onTick, delay) {
```json package.json hidden
{
"dependencies": {
"react": "experimental",
"react-dom": "experimental",
"react": "canary",
"react-dom": "canary",
"react-scripts": "latest"
},
"scripts": {
Expand Down Expand Up @@ -2324,7 +2326,7 @@ export function useCounter(delay) {

```js src/useInterval.js active
import { useEffect } from 'react';
import { experimental_useEffectEvent as useEffectEvent } from 'react';
import { useEffectEvent } from 'react';

export function useInterval(callback, delay) {
const onTick = useEffectEvent(callback);
Expand Down
Loading