【问题标题】:Uncaught Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refsUncaught Invariant Violation: addComponentAsRefTo(...): 只有 ReactOwner 可以有 refs
【发布时间】:2016-05-31 17:55:00
【问题描述】:

我正在尝试使用 react-data-grid (http://adazzle.github.io/react-data-grid/index.html) 但我不断收到错误:

invariant.js:17 Uncaught Invariant Violation: 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.

我已经尝试卸载软件包并重新安装无济于事。

这是我的代码:

render() {
    ...
    var _rows = [];
        for (var i = 1; i < 1000; i++) {
            _rows.push({
                id: i,
                title: 'Title ' + i,
                count: i * 1000
            });
        }

        var rowGetter = function(i){
            return _rows[i];
        };

        const columns = [
            {
                key: 'id',
                name: 'ID'
            },
            {
                key: 'title',
                name: 'Title'
            },
            {
                key: 'count',
                name: 'Count'
            }
        ]
   return (
        ...
<ReactDataGrid
                                columns={columns}
                                rowGetter={rowGetter}
                                rowsCount={_rows.length}
                                minHeight={277} />
...
   )
}

我该如何解决这个问题?


我修改了我的 config.js:

...
"npm:react-data-grid@0.14.42": {
      "classnames": "npm:classnames@1.2.2",
      "es5-shim": "npm:es5-shim@4.5.8",
      "fbjs": "npm:fbjs@0.6.1",
      "object-assign": "npm:object-assign@2.1.1",
      "process": "github:jspm/nodelibs-process@0.1.2",
      //"react": "npm:react@0.14.8",
      "react-contextmenu": "npm:react-contextmenu@1.5.0",
      //"react-dom": "npm:react-dom@0.14.8",
      "ron-react-autocomplete": "npm:ron-react-autocomplete@3.0.1"
    },
...

现在它可以工作了。

【问题讨论】:

    标签: javascript reactjs ecmascript-6


    【解决方案1】:

    或者你加载了多个 React 副本。

    每次我得到这个错误都是因为我已经加载了两次反应,确保你只加载一次反应,特别是如果你正在使用 webpack

    【讨论】:

    • 谢谢,看我的编辑。但我现在得到:警告:行:key 不是道具。尝试访问它会导致undefined 被返回。如果您需要在子组件中访问相同的值,则应将其作为不同的 prop 传递。
    猜你喜欢
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多