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
* `ref`: `Реф`, який ви отримали як проп у компоненті `MyInput`.
42
42
43
-
* `createHandle`: A function that takes no arguments and returns the ref handle you want to expose. That ref handle can have any type. Usually, you will return an object with the methods you want to expose.
43
+
* `createHandle`: Функція, яка не приймає аргументів і повертає об'єкт посилання, який ви хочете надати. Цей об’єкт може бути будь-якого типу. Зазвичай, ви повертатимете об'єкт з методами, які ви захочете використовувати.
44
44
45
-
* **optional** `dependencies`: The list of all reactive values referenced inside of the `createHandle` code. Reactive values include props, state, and all the variables and functions declared directly inside your component body. If your linter is [configured for React](/learn/editor-setup#linting), it will verify that every reactive value is correctly specified as a dependency. The list of dependencies must have a constant number of items and be written inline like `[dep1, dep2, dep3]`. React will compare each dependency with its previous value using the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. If a re-render resulted in a change to some dependency, or if you omitted this argument, your`createHandle`function will re-execute, and the newly created handle will be assigned to the ref.
45
+
* **Опціональний параметр** `dependencies`: Список усіх реактивних значень, на які посилається код `createHandle`. Реактивні значення включають в себе пропси, стан та всі змінні та функції, оголошені безпосередньо в тілі компонента. Якщо ваш лінтер [налаштований для Реакту](/learn/editor-setup#linting), він перевірить, чи кожне реактивне значення вказане як залежність. Список залежностей повинен містити стале число елементів, записаних в рядок як `[залежність1, залежність2, залежність3]`. Реакт порівняє кожну залежність із своїм попереднім значенням використовуючи порівняння [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is). Якщо повторний рендер призвів до зміни однієї із залежностей або якщо ви пропустили даний аргумент, ваша `createHandle` функція буде виконана повторно і новостворений об'єкт посилання буде призначений до рефу.
46
46
47
47
<Note>
48
48
49
-
Starting with React 19, [`ref` is available as a prop.](/blog/2024/12/05/react-19#ref-as-a-prop) In React 18 and earlier, it was necessary to get the `ref` from [`forwardRef`.](/reference/react/forwardRef)
49
+
Починаючи з React 19 [`реф` доступний як проп.](/blog/2024/12/05/react-19#ref-as-a-prop) У React 18 і старіших версіях необхідно було отримувати `реф` з [`forwardRef`.](/reference/react/forwardRef)
50
50
51
51
</Note>
52
52
53
-
#### Returns {/*returns*/}
53
+
#### Результат {/*returns*/}
54
54
55
-
`useImperativeHandle`returns`undefined`.
55
+
`useImperativeHandle`повертає`undefined`.
56
56
57
57
---
58
58
59
-
## Usage {/*usage*/}
59
+
## Використання {/*usage*/}
60
60
61
-
### Exposing a custom ref handle to the parent component {/*exposing-a-custom-ref-handle-to-the-parent-component*/}
61
+
### Надання кастомного об'єкта посилання батьківському компоненту {/*exposing-a-custom-ref-handle-to-the-parent-component*/}
62
62
63
-
To expose a DOM node to the parent element, pass in the `ref` prop to the node.
63
+
Щоб надати DOM-вузол батьківському елементу, передайте проп ref до цього вузла.
64
64
65
65
```js {2}
66
66
functionMyInput({ ref }) {
67
67
return<input ref={ref} />;
68
68
};
69
69
```
70
70
71
-
With the code above, [a ref to `MyInput`will receive the`<input>` DOM node.](/learn/manipulating-the-dom-with-refs) However, you can expose a custom value instead. To customize the exposed handle, call`useImperativeHandle`at the top level of your component:
71
+
У коді вище [посилання до `MyInput`отримає DOM вузол`<input>`.](/learn/manipulating-the-dom-with-refs) Однак, замість цього ви можете передати кастомне значення. Щоб кастомізувати наданий об'єкт посилання, викличте`useImperativeHandle`на верхньому рівні вашого компонента:
72
72
73
73
```js {4-8}
74
74
import { useImperativeHandle } from'react';
75
75
76
76
functionMyInput({ ref }) {
77
77
useImperativeHandle(ref, () => {
78
78
return {
79
-
// ... your methods ...
79
+
// ... ваші методи ...
80
80
};
81
81
}, []);
82
82
83
83
return<input />;
84
84
};
85
85
```
86
86
87
-
Note that in the code above, the `ref` is no longer passed to the`<input>`.
87
+
Зверніть увагу, що в наданому вище коді `реф` більше не передається до`<input>`.
88
88
89
89
For example, suppose you don't want to expose the entire `<input>` DOM node, but you want to expose two of its methods: `focus` and `scrollIntoView`. To do this, keep the real browser DOM in a separate ref. Then use `useImperativeHandle` to expose a handle with only the methods that you want the parent component to call:
0 commit comments