2015-12-11 31 views
3

我想與<過濾/>從onChange事件到父組件< ViewTwoComponent組件共享數據/>我不知道該怎麼辦呢React.js如何找到ref元素在內部組件

你知道也許如何在組件和他的父母之間共享數據

ViewTwoComponent他們看不到ref值,我不知道爲什麼?

在控制檯上的錯誤:遺漏的類型錯誤:無法讀取的不確定

var ViewTwoComponent = React.createClass({ 
    "getInitialState": function() { 
     return { 
      "userTextValue": "hello11111111111111", 
      "userTextRef": "userTextRef" 
     } 
    }, 
    "updateState": function(value) { 
     this.setState({userTextValue: value }) 
    }, 
    "handleChange": function() { 
     this.updateState(this.refs.userTextRef.getDOMNode().value) 
    }, 
    "render": function() { 
     return <div> 
      <Inner /> 
      <Filtering refName={this.state.userTextRef} handleChange={this.handleChange} userTextValue={this.state.userTextValue} /> 
     </div>; 
    } 
}) 

var Inner = React.createClass({ 
    "render": function() { 
     return <span>INNER</span>; 
    } 
}); 

var Filtering = React.createClass({ 
    "render": function() { 
     return <span> 
      <input type="text" ref={this.props.refName} onChange={this.props.handleChange} value={this.props.userTextValue} /> 
     </span>; 
    } 
}); 

React.render(< ViewTwoComponent />, document.getElementById("inner")) 
+0

誰能幫助我? :) – Olo

回答

0

財產「getDOMNode」這是一個有點混亂,你的意思是,當你嘗試引用this.refs.userTextRef但我假設你想要的值在你的狀態。我還會假設密鑰userTextRef的值實際上不是userTextRef。您可以嘗試使用方括號來訪問該值。

"handleChange": function() { 
    this.updateState(this.refs[userTextRef]getDOMNode().value) 
    } 
+0

我想分享數據與從onChange事件組件到父組件我不知道該怎麼做。 – Olo

+0

不幸的是,您的解決方案無效:/ – Olo

0

我是新來的反應,但是從我記得教程,你應該傳遞Filtering成分的功能到ViewTwoComponent

var Filtering = React.createClass({ 
     "childChanged" : function(child, value) { 
      console.log("child: " + child + "change value to: " value); 
     }, 
     "render": function() { 
      return <span> 
       <input type="text" ref={this.props.refName} onChange={this.props.handleChange} value={this.props.userTextValue} notifyParent={this.childChanged} /> 
      </span>; 
     } 
    }); 

,然後在ViewTwoComponent類的handleChange方法,你叫this.props.notifyParent(this, this.state.userTextValue);

相關問題