Javascript React.js如何在内部组件中查找ref元素
我想与Javascript React.js如何在内部组件中查找ref元素,javascript,reactjs,Javascript,Reactjs,我想与组件共享从onChange事件到父组件的数据我不知道该怎么做 您知道如何在组件和其父组件之间共享数据吗 ViewTwoComponent他们看不到ref值,我不知道为什么 控制台上出现错误:未捕获的TypeError:无法读取未定义的属性“getDOMNode” var viewtowcomponent=React.createClass({ “getInitialState”:函数(){ 返回{ “userTextValue”:“Hello11111111”, “userTextRef”
var viewtowcomponent=React.createClass({
“getInitialState”:函数(){
返回{
“userTextValue”:“Hello11111111”,
“userTextRef”:“userTextRef”
}
},
“更新状态”:功能(值){
this.setState({userTextValue:value})
},
“handleChange”:函数(){
this.updateState(this.refs.userTextRef.getDOMNode().value)
},
“render”:函数(){
返回
;
}
})
var Inner=React.createClass({
“render”:函数(){
返回内部;
}
});
var Filtering=React.createClass({
“render”:函数(){
返回
;
}
});
React.render( ,document.getElementById(“内部”))
当您尝试引用this.refs.userTextRef
时,您的意思有点混乱,但我假设您想要处于您的状态的值。我还将假设键userTextRef
的值实际上不是userTextRef
。您可以尝试使用方括号访问该值
"handleChange": function() {
this.updateState(this.refs[userTextRef]getDOMNode().value)
}
我是新手,但根据我对教程的记忆,您应该将
过滤
组件的函数传递给视图组件
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>;
}
});
var筛选=React.createClass({
“childChanged”:函数(子级,值){
log(“child:+child+”将值更改为:“value”);
},
“render”:函数(){
返回
;
}
});
然后,在ViewTwoComponent
类的handleChange
方法中,调用this.props.notifyParent(this,this.state.userTextValue)代码>我想与组件共享从onChange事件到父组件的数据,但我不知道如何实现。
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>;
}
});