Skip to content

Commit 2cf24df

Browse files
committed
docs(hooks): useDebugValue
1 parent 6810068 commit 2cf24df

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

src/content/reference/react/useDebugValue.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: useDebugValue
44

55
<Intro>
66

7-
`useDebugValue` is a React Hook that lets you add a label to a custom Hook in [React DevTools.](/learn/react-developer-tools)
7+
`useDebugValue` 是一個 React Hook,讓你為客製化 Hook [React 開發者工具](/learn/react-developer-tools) 中新增標籤(label)。
88

99
```js
1010
useDebugValue(value, format?)
@@ -16,54 +16,54 @@ useDebugValue(value, format?)
1616
1717
---
1818
19-
## Reference {/*reference*/}
19+
## 參考 {/*reference*/}
2020
2121
### `useDebugValue(value, format?)` {/*usedebugvalue*/}
2222
23-
Call `useDebugValue` at the top level of your [custom Hook](/learn/reusing-logic-with-custom-hooks) to display a readable debug value:
23+
在 [客製化 Hook](/learn/reusing-logic-with-custom-hooks) 的頂層呼叫 `useDebugValue` 以顯示可讀的除錯值(debug value):
2424
2525
```js
2626
import { useDebugValue } from 'react';
2727

2828
function useOnlineStatus() {
2929
// ...
30-
useDebugValue(isOnline ? 'Online' : 'Offline');
30+
useDebugValue(isOnline ? '在線' : '離線');
3131
// ...
3232
}
3333
```
3434
35-
[See more examples below.](#usage)
35+
[查看更多下方的範例。](#usage)
3636
37-
#### Parameters {/*parameters*/}
37+
#### 參數 {/*parameters*/}
3838
39-
* `value`: The value you want to display in React DevTools. It can have any type.
40-
* **optional** `format`: A formatting function. When the component is inspected, React DevTools will call the formatting function with the `value` as the argument, and then display the returned formatted value (which may have any type). If you don't specify the formatting function, the original `value` itself will be displayed.
39+
* `value`:你希望顯示在 React 開發者工具的值。可以是任何型別。
40+
* **可選的** `format`:一個格式化(formatting)函式。當元件被查看時,React 開發者工具會以 `value` 作為引數(argument)呼叫格式化函式,接著顯示回傳的格式化數值(可能是任何型別)。如果沒有指定格式化函式,會顯示原本的 `value` 本身。
4141
42-
#### Returns {/*returns*/}
42+
#### 回傳值 {/*returns*/}
4343
44-
`useDebugValue` does not return anything.
44+
`useDebugValue` 不回傳任何東西。
4545
46-
## Usage {/*usage*/}
46+
## 使用 {/*usage*/}
4747
48-
### Adding a label to a custom Hook {/*adding-a-label-to-a-custom-hook*/}
48+
### 新增標籤給客製化 Hook {/*adding-a-label-to-a-custom-hook*/}
4949
50-
Call `useDebugValue` at the top level of your [custom Hook](/learn/reusing-logic-with-custom-hooks) to display a readable <CodeStep step={1}>debug value</CodeStep> for [React DevTools.](/learn/react-developer-tools)
50+
在[客製化 Hook](/learn/reusing-logic-with-custom-hooks) 的頂層呼叫 `useDebugValue` ,讓 [React 開發者工具](/learn/react-developer-tools) 顯示可讀的 <CodeStep step={1}>除錯值</CodeStep>。
5151
52-
```js [[1, 5, "isOnline ? 'Online' : 'Offline'"]]
52+
```js [[1, 5, "isOnline ? '在線' : '離線'"]]
5353
import { useDebugValue } from 'react';
5454

5555
function useOnlineStatus() {
5656
// ...
57-
useDebugValue(isOnline ? 'Online' : 'Offline');
57+
useDebugValue(isOnline ? '在線' : '離線');
5858
// ...
5959
}
6060
```
6161
62-
This gives components calling `useOnlineStatus` a label like `OnlineStatus: "Online"` when you inspect them:
62+
這樣當你查看呼叫 `useOnlineStatus` 的元件時,會顯示類似 `OnlineStatus: "在線"` 的標籤:
6363
64-
![A screenshot of React DevTools showing the debug value](/images/docs/react-devtools-usedebugvalue.png)
64+
![展示除錯值的 React 開發者工具截圖](/images/docs/react-devtools-usedebugvalue.png)
6565
66-
Without the `useDebugValue` call, only the underlying data (in this example, `true`) would be displayed.
66+
沒有呼叫 `useDebugValue` 的話,只會顯示基本的資料(在這個範例中是 `true`)。
6767
6868
<Sandpack>
6969
@@ -72,7 +72,7 @@ import { useOnlineStatus } from './useOnlineStatus.js';
7272

7373
function StatusBar() {
7474
const isOnline = useOnlineStatus();
75-
return <h1>{isOnline ? 'Online' : 'Disconnected'}</h1>;
75+
return <h1>{isOnline ? '在線' : '連線中斷'}</h1>;
7676
}
7777

7878
export default function App() {
@@ -85,7 +85,7 @@ import { useSyncExternalStore, useDebugValue } from 'react';
8585

8686
export function useOnlineStatus() {
8787
const isOnline = useSyncExternalStore(subscribe, () => navigator.onLine, () => true);
88-
useDebugValue(isOnline ? 'Online' : 'Offline');
88+
useDebugValue(isOnline ? '在線' : '離線');
8989
return isOnline;
9090
}
9191

@@ -103,20 +103,21 @@ function subscribe(callback) {
103103
104104
<Note>
105105
106-
Don't add debug values to every custom Hook. It's most valuable for custom Hooks that are part of shared libraries and that have a complex internal data structure that's difficult to inspect.
106+
不要在每個客製化 Hook 都加上除錯值。它最適合用在共享函式庫中的客製化 Hook,特別是那些具有難以檢查的複雜內部資料結構的 Hook。
107107
108108
</Note>
109109
110110
---
111111
112-
### Deferring formatting of a debug value {/*deferring-formatting-of-a-debug-value*/}
112+
### 延遲除錯值的格式化 {/*deferring-formatting-of-a-debug-value*/}
113113
114-
You can also pass a formatting function as the second argument to `useDebugValue`:
114+
你可以將格式化函式作為 `useDebugValue` 的第二個引數傳入:
115115
116116
```js [[1, 1, "date", 18], [2, 1, "date.toDateString()"]]
117117
useDebugValue(date, date => date.toDateString());
118118
```
119119
120-
Your formatting function will receive the <CodeStep step={1}>debug value</CodeStep> as a parameter and should return a <CodeStep step={2}>formatted display value</CodeStep>. When your component is inspected, React DevTools will call this function and display its result.
120+
你的格式化函式會收到 <CodeStep step={1}>除錯值</CodeStep> 作為參數,並回傳 <CodeStep step={2}>格式化後的顯示值</CodeStep>。當元件被查看時,React 開發者工具會呼叫這個函式並顯示結果。
121+
122+
這樣可以避免執行可能耗費效能的格式化邏輯,除非元件真的被查看。舉例來說,如果 `date` 是 Date 值,可以避免每次渲染都對它呼叫 `toDateString()`
121123
122-
This lets you avoid running potentially expensive formatting logic unless the component is actually inspected. For example, if `date` is a Date value, this avoids calling `toDateString()` on it for every render.

0 commit comments

Comments
 (0)