[英]Only a ReactOwner can have refs
I am receiving this error: 我收到此错误:
Error: (SystemJS) addComponentAsRefTo(...): Only a ReactOwner can have refs.
错误:(SystemJS)addComponentAsRefTo(...):只有ReactOwner可以具有引用。 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.您可能正在向未在组件的
render
方法中创建的组件添加引用,或者您已加载多个React副本。
The network tab on the browser does not show any duplicates being loaded. 浏览器上的“网络”选项卡不显示任何正在加载的重复项。
Here is my component ( TSX
): 这是我的组件(
TSX
):
import * as DOM from "react-dom";
import * as $ from "jquery";
import * as React from "react";
declare var layerslider;
export class LayerSlider extends React.Component<any, {}> {
element: any;
componentDidMount() {
console.log(this.element);
}
render() {
return <div>
{this.props.children}
</div>;
}
}
DOM.render(<div><div ref="element"></div></div>, document.getElementById("lsl"));
I am using system.js as follows. 我正在使用system.js如下。
System.config({
typescriptOptions: { emitDecoratorMetadata: true },
map: {
'react-component': 'ignite/components',
'jquery': 'lib/jquery/dist/jquery.js',
},
paths: {
'react-dom': "lib-npm/react-dom/dist/react-dom.js",
'react': "lib-npm/react/dist/react.js"
},
packages: {
app: {
defaultExtension: 'js'
},
'ignite': { main: 'boot.js' },
'rxjs': { main: 'Rx.js' },
}
});
the html is... 的HTML是...
<layerslider id="lsl">
<div class="layerslider" style="width: 100%; height: 1200px">
<div class="ls-slide">
<img src="~/images/site/home/banner.jpg" class="ls-bg" alt="Image layer">
<h4 class="ls-l" style="top: 40%; left: 50%; width: 80%; text-align: center" data-ls="@ViewBag.Center">Recognizing your commitment to</h4>
<h1 class="ls-l green" style="top: 55%; left: 50%; width: 80%; text-align: center" data-ls="@ViewBag.Center">SUSTAINABLE ENERGY</h1>
</div>
</div>
</layerslider>
...
The heart of the error you are getting: 您得到的错误的核心:
Only a ReactOwner can have refs.
只有ReactOwner可以具有引用。
You should not have a ref
if the render
is happing outside a react component. 如果
render
器在 react组件外部进行封装,则不应具有ref
。 This is the case in your code: 在您的代码中就是这种情况:
DOM.render(<div><div ref="element"></div></div>, document.getElementById("lsl"));
Remove the ref
so that you have : 删除
ref
以便拥有:
DOM.render(<div><div></div></div>, document.getElementById("lsl"));
Move the ref from DOM.render
into the component to obtain the component reference. 将引用从
DOM.render
移动到组件中以获得组件引用。
export class LayerSlider extends React.Component<any, {}> {
...
element: any;
render() {
// the ref needs to be *here*
return <div ref="element">
{this.props.children}
</div>;
}
}
Render cannot be in the html
below. 渲染不能在以下
html
中。
DOM.render(
<div></div>,
document.getElementById("lsl"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.