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/useImperativeHandle.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,7 +86,7 @@ function MyInput({ ref }) {
86
86
87
87
Зверніть увагу, що в наданому вище коді `реф` більше не передається до `<input>`.
88
88
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:
89
+
Наприклад, припустимо, ви не хочете використовувати весь вузол DOM `<input>`, а лише два його методи: `focus`і`scrollIntoView`. Щоб зробити це, зберігайте справжній браузерний DOM у окремому посиланні. Потім викличте`useImperativeHandle`щоб повернути об'єкт, який містить лише ті методи, які ви хочете, щоб батьківський компонент викликав:
Now, if the parent component gets a ref to `MyInput`, it will be able to call the `focus`and`scrollIntoView`methods on it. However, it will not have full access to the underlying`<input>` DOM node.
112
+
Тепер, якщо батьківський компонент передасть реф до `MyInput`, він буде здатний викликати методи `focus`і`scrollIntoView`в компоненті. Однак, він не буде мати повного доступу до DOM вузла`<input>`.
113
113
114
114
<Sandpack>
115
115
@@ -122,7 +122,7 @@ export default function Form() {
122
122
123
123
functionhandleClick() {
124
124
ref.current.focus();
125
-
//This won't work because the DOM node isn't exposed:
125
+
//Це не спрацює, бо вузол DOM не був представлений:
126
126
// ref.current.style.opacity = 0.5;
127
127
}
128
128
@@ -170,9 +170,9 @@ input {
170
170
171
171
---
172
172
173
-
### Exposing your own imperative methods {/*exposing-your-own-imperative-methods*/}
173
+
### Передавання власних імперативних методів {/*exposing-your-own-imperative-methods*/}
174
174
175
-
The methods you expose via an imperative handle don't have to match the DOM methods exactly. For example, this `Post`component exposes a `scrollAndFocusAddComment`method via an imperative handle. This lets the parent`Page`scroll the list of comments *and* focus the input field when you click the button:
175
+
Методи, які ви передаєте через імперативний обробник не обов'язково мають точно відповідати DOM методам. Наприклад, даний компонент `Post`передає метод `scrollAndFocusAddComment`через імперативний обробник. Це дозволяє батьківському компоненту`Page`гортати список коментарів *і* фокусуватись на полі введення, коли ви натискаєте кнопку:
176
176
177
177
<Sandpack>
178
178
@@ -285,8 +285,8 @@ export default AddComment;
285
285
286
286
<Pitfall>
287
287
288
-
**Do not overuse refs.** You should only use refs for *imperative* behaviors that you can't express as props: for example, scrolling to a node, focusing a node, triggering an animation, selecting text, and so on.
288
+
**Не зловживайте рефами.** Використовуйте рефи лише для *імперативної* поведінки, яку ви не можете виразити через пропси: наприклад, пролистування до вузла DOM, фокусування на вузлі, виклик анімації, виділення тексту тощо.
289
289
290
-
**If you can express something as a prop, you should not use a ref.** For example, instead of exposing an imperative handle like `{ open, close }`from a`Modal` component, it is better to take`isOpen`as a prop like `<Modal isOpen={isOpen} />`. [Effects](/learn/synchronizing-with-effects) can help you expose imperative behaviors via props.
290
+
**Якщо ви можете виразити щось як проп, тоді не варто використовувати реф.** Наприклад, замість передачі імперативного обробника у вигляді `{ open, close }`з компоненту`Modal`, кращим підходом буде використати проп`isOpen`у такому вигляді `<Modal isOpen={isOpen} />`. [Effects](/learn/synchronizing-with-effects) може допомогти вам задавати імперативну поведінку через пропси.
0 commit comments