简体   繁体   English

只有ReactOwner可以具有引用

[英]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可以具有引用。

Fix 固定

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.

相关问题 未捕获的不变违反:addComponentAsRefTo(…):只有ReactOwner可以具有引用 - Uncaught Invariant Violation: addComponentAsRefTo(…): Only a ReactOwner can have refs addComponentAsRefTo(…):只有ReactOwner可以拥有refs-选择列表形式的错误 - addComponentAsRefTo(…): Only a ReactOwner can have refs - Error in Form with Select List 测试:`错误:永久违反:addComponentAsRefTo(...):只有ReactOwner可以具有引用。`错误 - Testing: `Error: Invariant Violation: addComponentAsRefTo(…): Only a ReactOwner can have refs.` error React组件可以在浏览器中正确渲染,但是Jest在渲染时会测试错误:“只有ReactOwner可以具有引用” - React components render correctly in browser, but Jest test errors when rendering: “Only a ReactOwner can have refs” 只有ReactOwner可以有refs。 您可能正在向组件的render方法中未创建的组件添加ref - 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 在ReactJS中,我的代码是否有支持实例,因此可以使用引用? - In ReactJS does my code have backing instances and therefore can use refs? 如何使子组件的子组件具有第一级子代可以使用的引用? - How make child components of a child component have refs that the first-level child can use? 无法访问ComponentDidMount上的引用 - Can't access refs on ComponentDidMount 错误:Function 组件不能有参考。? - Error : Function components cannot have refs.? 输入的多个参考,仅适用于最后一个 - multiple refs for inputs, works only last one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM