我只是想问我如何从其他类访问 this.state.sampleString .. 这是我的代码
class MainClass extends Component {
constructor(props){
super(props)
this.state = {
sampleString: 'Test String'
}
this.getValue = this.getValue.bind(this);
}
getValue(){
//console.log(this.state.sampleString);
return this.state.sampleString
}
}
==========
这是我的第二个类的函数,用于从 MainClass 获取“this.state.sampleString”的值
function getValueFromMainClass() {
var stringFromClassHeader = () => {HeaderWithBg.getValue()}
console.log(stringFromClassHeader.sampleString);
}
为什么它返回“未定义”?
非常感谢。我是 React Native 的新手。