diff --git a/src/content/learn/manipulating-the-dom-with-refs.md b/src/content/learn/manipulating-the-dom-with-refs.md index b5c193763..d62061ed1 100644 --- a/src/content/learn/manipulating-the-dom-with-refs.md +++ b/src/content/learn/manipulating-the-dom-with-refs.md @@ -1,52 +1,52 @@ --- -title: 'Manipulating the DOM with Refs' +title: 'Манипулирование DOM с помощью ссылок' --- -React automatically updates the [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction) to match your render output, so your components won't often need to manipulate it. However, sometimes you might need access to the DOM elements managed by React--for example, to focus a node, scroll to it, or measure its size and position. There is no built-in way to do those things in React, so you will need a *ref* to the DOM node. +React автоматически обновляет [DOM](https://developer.mozilla.org/docs/Web/API/Document_Object_Model/Introduction ) в соответствии с вашими результатами рендеринга, так что вашим компонентам не нужно будет часто манипулировать ими. Однако иногда вам может понадобиться доступ к элементам DOM, управляемым React, например, чтобы сфокусировать узел, прокрутить до него или измерить его размер и положение. В React нет встроенного способа выполнить эти действия, поэтому вам понадобится * ссылка * на узел DOM. -- How to access a DOM node managed by React with the `ref` attribute -- How the `ref` JSX attribute relates to the `useRef` Hook -- How to access another component's DOM node -- In which cases it's safe to modify the DOM managed by React +- Как получить доступ к узлу DOM, управляемому React, с атрибутом `ref` +- Как атрибут JSX `ref` соотносится с хук `use Reef` +- Как получить доступ к DOM-узлу другого компонента +- В каких случаях безопасно изменять DOM, управляемый React -## Getting a ref to the node {/*getting-a-ref-to-the-node*/} +## Получение ссылки на узел {/*getting-a-ref-to-the-node*/} -To access a DOM node managed by React, first, import the `useRef` Hook: +Чтобы получить доступ к узлу DOM, управляемому React, сначала импортируйте `user` Крюк: ```js import { useRef } from 'react'; ``` -Then, use it to declare a ref inside your component: +Затем используйте его для объявления ссылки внутри вашего компонента: ```js const myRef = useRef(null); ``` -Finally, pass it to the DOM node as the `ref` attribute: +Наконец, передайте свой ref в качестве атрибута `ref` тегу JSX, для которого вы хотите получить узел DOM: ```js
``` -The `useRef` Hook returns an object with a single property called `current`. Initially, `myRef.current` will be `null`. When React creates a DOM node for this `
`, React will put a reference to this node into `myRef.current`. You can then access this DOM node from your [event handlers](/learn/responding-to-events) and use the built-in [browser APIs](https://developer.mozilla.org/docs/Web/API/Element) defined on it. +Крюк `use Reef` возвращает объект с единственным свойством под названием `current`. Изначально `myRef.current` будет иметь значение `null`. Когда React создаст узел DOM для этого `
`, React поместит ссылку на этот узел в `myRef.current`. Затем вы можете получить доступ к этому узлу DOM из ваших [обработчиков событий](/learn/responsing-to-events) и использовать встроенные [API браузера](https://developer.mozilla.org/docs/Web/API/Element ) определено на нем. ```js -// You can use any browser APIs, for example: +// Вы можете использовать любые API-интерфейсы браузера, например: myRef.current.scrollIntoView(); ``` -### Example: Focusing a text input {/*example-focusing-a-text-input*/} +### Пример: Фокусировка ввода текста {/*example-focusing-a-text-input*/} -In this example, clicking the button will focus the input: +В этом примере нажатие кнопки сфокусирует вводимые данные: @@ -73,18 +73,17 @@ export default function Form() { -To implement this: +Чтобы реализовать это: -1. Declare `inputRef` with the `useRef` Hook. -2. Pass it as ``. This tells React to **put this ``'s DOM node into `inputRef.current`.** -3. In the `handleClick` function, read the input DOM node from `inputRef.current` and call [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) on it with `inputRef.current.focus()`. -4. Pass the `handleClick` event handler to `