2015-11-03 29 views
0

我用我的陣營0.13應用這樣的方法subrender(分裂的助手方法反應組件的創建)。但是,我得到了一個錯誤:參考文獻如何作用於反應0.14

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

那麼,怎樣才能管理上創建反應依賴於DOM組件飛我避免發胖我的主要呈現方式?

任何線索如何重構是在0.14的新辦法作出反應的版本?

module.exports = React.createClass({ 
displayName: 'Typed', 
render: function() { 
    var _this = this; 

    return (
     React.createElement("div", { 
      style: { 
       position: 'relative' 
      }, 
      className: 'react-typeahead-container ' + _this.props.className}, 
     _this._renderInput(), 
     _this._renderDropdown(), 
     _this._renderAriaMessageForOptions(), 
     _this._renderAriaMessageForIncomingOptions() 
    ) 
    ); 
}, 
_renderInput: function() { 
    var _this = this, 
     state = _this.state, 
     props = _this.props, 
     inputValue = props.inputValue, 
     inputValue = props.inputValue, 
     className = 'react-typeahead-input', 
     inputDirection = getTextDirection(inputValue); 

    return (
     React.createElement("div", { 
      style: { 
       position: 'relative' 
      }, 
      className: "react-typeahead-input-container"}, 
      React.createElement(Input, { 
       ref: "input", //this does not works :(
       role: "combobox" 
      ) 
     ) 
    ); 
}, 
+0

順便說一句,您是否需要_React_模塊? –

+0

當然兄弟,陣營模塊它之前必需的,但感謝你 – OsDev

+0

我沒有面對它。但是這裏(http://stackoverflow.com/questions/28519287/what-does-only-a-reactowner-can-have -refs-mean)是類似的問題。我希望它能幫助你 –

回答

1

從我的理解,ref應該是接收引用的元素的回調方法。在這種情況下,input。然後,您可以將其存儲在state或其他人身上並引用它。當它有效時,被引用的DOM節點將被渲染。

+0

這是在0.13中引入的,並且是可選的。基於字符串的引用仍然可以工作(至少對於基於'createClass'的組件) –

1

作爲this JSFiddle shows,你有上面的代碼工作(這樣它會作爲書面略有修改)。有一個小的語法錯誤;包含屬性的對象缺少一個閉合的大括號:

React.createElement(Input, { 
    ref: "input", //this does not works :(
    role: "combobox" 
) // <-- should be })