如何从本地的反馈中获取其他组件的this.state.value [英] How do I get a this.state.value from other components in react native
本文介绍了如何从本地的反馈中获取其他组件的this.state.value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我把它从classone中调用classtwo中的getValue,但是还有另一种方法呢?有没有更简单的方法来从classtwo获取this.state.value?
I got it to work calling getValue in classtwo from classone but is there another way of doing this? Is there a easier way to get this.state.value from classtwo?
我尝试将静态getValue放在静态,但总是会给我一个错误。任何人都可以帮忙
I tried putting static getValue as static but it always throw me an error. Anyone can help?
谢谢!
class ClassOne extends React.Component {
constructor(props) {
super(props);
this.state = {
classtwo: new ClassTwo(),
};
}
CallGetValue(){
this.state.classtwo.getValue();
}
}
class ClassTwo extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 100,
};
}
getValue(){
return this.state.value;
}
}
推荐答案
应该看起来像这样。
class ClassOne extends React.Component {
constructor(props) {
super(props);
this.state = {
classtwo: 0,
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.classtwo && (this.state.classtwo != nextProps.classtwo))
{this.setState({classtwo: nextProps.classtwo});}
}
}
class ClassTwo extends React.Component {
constructor(props) {
super(props);
this.changeValue = this. changeValue.bind(this);
this.state = {
value: 100,
};
}
changeValue(value){
this.props.changeValue(this.state.value);
this.setState(value: value);
}
}
class ClassZero extends React.Component {
constructor(props) {
super(props);
this.changeValue = this. changeValue.bind(this);
this.state = {
classTwoValue: 0,
};
}
changeValue(value){
this.setState(classTwoValue: value);
}
render() {
return (
<div>
<ClassOne classtwo={this.state.classTwoValue}/>
<ClassTwo changeValue={this.changeValue}/>
</div>
)
}
}
这篇关于如何从本地的反馈中获取其他组件的this.state.value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文