You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/reference/react/useEffectEvent.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ title: useEffectEvent
4
4
5
5
<Intro>
6
6
7
-
`useEffectEvent`is a React Hook that lets you extract non-reactive logic from your Effects into a reusable function called an [Effect Event](/learn/separating-events-from-effects#declaring-an-effect-event).
Call`useEffectEvent`at the top level of your component to declare an Effect Event. Effect Events are functions you can call inside Effects, such as `useEffect`:
@@ -41,33 +41,33 @@ function ChatRoom({ roomId, theme }) {
41
41
}
42
42
```
43
43
44
-
[See more examples below.](#usage)
44
+
[さらに例を見る](#usage)
45
45
46
-
#### Parameters {/*parameters*/}
46
+
#### 引数 {/*parameters*/}
47
47
48
-
-`callback`: A function containing the logic for your Effect Event. When you define an Effect Event with `useEffectEvent`, the `callback`always accesses the latest values from props and state when it is invoked. This helps avoid issues with stale closures.
48
+
-`callback`: エフェクトイベントのロジックを含む関数。`useEffectEvent` でエフェクトイベントを定義すると、`callback`は常に、呼び出された瞬間の props や state の最新の値にアクセスします。これにより、古くなったクロージャに関する問題を回避できます。
49
49
50
-
#### Returns {/*returns*/}
50
+
#### 返り値 {/*returns*/}
51
51
52
-
Returns an Effect Event function. You can call this function inside `useEffect`, `useLayoutEffect`, or `useInsertionEffect`.
-**Only call inside Effects:** Effect Events should only be called within Effects. Define them just before the Effect that uses them. Do not pass them to other components or hooks. The [`eslint-plugin-react-hooks`](/reference/eslint-plugin-react-hooks)linter (version 6.1.1 or higher) will enforce this restriction to prevent calling Effect Events in the wrong context.
57
-
-**Not a dependency shortcut:** Do not use `useEffectEvent`to avoid specifying dependencies in your Effect's dependency array. This can hide bugs and make your code harder to understand. Prefer explicit dependencies or use refs to compare previous values if needed.
58
-
-**Use for non-reactive logic:** Only use `useEffectEvent`to extract logic that does not depend on changing values.
### Reading the latest props and state {/*reading-the-latest-props-and-state*/}
64
+
### 最新の props と state を読み取る {/*reading-the-latest-props-and-state*/}
65
65
66
-
Typically, when you access a reactive value inside an Effect, you must include it in the dependency array. This makes sure your Effect runs again whenever that value changes, which is usually the desired behavior.
But in some cases, you may want to read the most recent props or state inside an Effect without causing the Effect to re-run when those values change.
68
+
しかし場合によっては、これらの値が変化してもエフェクトを再実行させることなく、エフェクト内で最新の props や state を読み取りたいことがあります。
69
69
70
-
To [read the latest props or state](/learn/separating-events-from-effects#reading-latest-props-and-state-with-effect-events) in your Effect, without making those values reactive, include them in an Effect Event.
70
+
エフェクト内で[最新の props や state を読み取る](/learn/separating-events-from-effects#reading-latest-props-and-state-with-effect-events)際に、それらの値をリアクティブにしないようにするには、エフェクトイベント内に含めます。
In this example, the Effect should re-run after a render when `url`changes (to log the new page visit), but it should **not** re-run when `numberOfItems`changes. By wrapping the logging logic in an Effect Event, `numberOfItems` becomes non-reactive. It's always read from the latest value without triggering the Effect.
You can pass reactive values like `url`as arguments to the Effect Event to keep them reactive while accessing the latest non-reactive values inside the event.
0 commit comments