Skip to content

Commit ec207b1

Browse files
committed
Предупреждение: Refs Must Have Owner
1 parent 42064e2 commit ec207b1

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
---
2-
title: Refs Must Have Owner Warning
2+
title: Предупреждение: Refs Must Have Owner
33
layout: single
44
permalink: warnings/refs-must-have-owner.html
55
---
66

7-
You are probably here because you got one of the following error messages:
7+
Вероятно, вы зашли на эту страницу потому, что получили одно из следующих сообщений об ошибке:
88

99
*React 16.0.0+*
1010
> Warning:
1111
>
1212
> Element ref was specified as a string (myRefName) but no owner was set. You may have multiple copies of React loaded. (details: https://fb.me/react-refs-must-have-owner).
1313
14-
*earlier versions of React*
14+
*более ранние версии React*
1515
> Warning:
1616
>
1717
> addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded.
1818
19-
This usually means one of three things:
19+
Обычно это означает одну из трёх вещей:
2020

21-
- You are trying to add a `ref` to a function component.
22-
- You are trying to add a `ref` to an element that is being created outside of a component's render() function.
23-
- You have multiple (conflicting) copies of React loaded (eg. due to a misconfigured npm dependency)
21+
- Вы пытаетесь добавить `ref` в функциональный компонент.
22+
- Вы пытаетесь добавить `ref` к элементу, который был создан вне метода `render()` текущего компонента.
23+
- У вас загружено несколько конфликтующих копий React (например из-за неправильной настройки npm зависимостей).
2424

25-
## Refs on Function Components {#refs-on-function-components}
25+
## Рефы в функциональных компонентах {#refs-on-function-components}
2626

27-
If `<Foo>` is a function component, you can't add a ref to it:
27+
Если `<Foo>` задан функциональным компонентом, то вы не можете на нем добавить `ref`:
2828

2929
```js
30-
// Doesn't work if Foo is a function!
30+
// Не работает, если Foo это фукнция!
3131
<Foo ref={foo} />
3232
```
3333

34-
If you need to add a ref to a component, convert it to a class first, or consider not using refs as they are [rarely necessary](/docs/refs-and-the-dom.html#when-to-use-refs).
34+
Если нужно добавить `ref` к компоненту, сначала преобразуйте его в класс, или не пользуйтесь механизмом рефов, потому что он нужен [достаточно редко](/docs/refs-and-the-dom.html#when-to-use-refs).
3535

36-
## Strings Refs Outside the Render Method {#strings-refs-outside-the-render-method}
36+
## Строковые рефы вне метода `render` {#strings-refs-outside-the-render-method}
3737

38-
This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's `render` method). For example, this won't work:
38+
Обычно это означает, что вы пытаетесь добавить реф к компонентy, который не имеет владельца (т.е. не был создан внутри метода `render` какого-либо компонента).
39+
40+
Следующий пример работать не будет:
3941

4042
```js
41-
// Doesn't work!
43+
// Не работает!
4244
ReactDOM.render(<App ref="app" />, el);
4345
```
4446

45-
Try rendering this component inside of a new top-level component which will hold the ref. Alternatively, you may use a callback ref:
47+
Попробуйте отрендерить этот компонент внутри нового компонента-обертки, который будет содержать реф. Как вариант - вы можете использовать колбэк-реф:
4648

4749
```js
4850
let app;
@@ -54,10 +56,10 @@ ReactDOM.render(
5456
);
5557
```
5658

57-
Consider if you [really need a ref](/docs/refs-and-the-dom.html#when-to-use-refs) before using this approach.
59+
Хорошо подумайте -- [правда ли вам нужен реф](/docs/refs-and-the-dom.html#when-to-use-refs) перед использованием этого подхода.
5860

59-
## Multiple copies of React {#multiple-copies-of-react}
61+
## Несколько копий React {#multiple-copies-of-react}
6062

61-
Bower does a good job of deduplicating dependencies, but npm does not. If you aren't doing anything (fancy) with refs, there is a good chance that the problem is not with your refs, but rather an issue with having multiple copies of React loaded into your project. Sometimes, when you pull in a third-party module via npm, you will get a duplicate copy of the dependency library, and this can create problems.
63+
Bower хорошо решает вопрос дублирования зависимостей, а вот npm нет. Если вы не делаете с вашими рефами ничего специфичного, есть большая вероятность, что проблема не в вашем коде, а в том, что в вашем проекте загружено несколько копий React. Иногда, когда вы устанавливаете сторонний пакет через npm, вы можете получить дублирование библиотеки из зависимостей и это может создать проблему.
6264

63-
If you are using npm... `npm ls` or `npm ls react` might help illuminate.
65+
Если вы используете npm ...`npm ls` или `npm ls react` может помочь разобраться.

0 commit comments

Comments
 (0)