我有一个按钮,它调用onApproximateAgePress过程,该过程无法看到“this.data”。为什么'this.data'超出范围?
<Button title="<20" onPress={onApproximateAgePress}/>
onApproximateAgePress() {
console.log('onApproximateAgePress') ; // outputs onApproximateAgePress
console.log(this.data) ; // outputs null
}
// Getter for "Immutable.js" state data...
get data() {
return this.state.data;
}
// Setter for "Immutable.js" state data...
set data(data) {
this.setState({ data });
}
state = {
data: fromJS({
showApproximateAgePicker: false,
...
}),
}
我有其他组件已经以类似的方式连接起来,但他们确实看到this.data。
如果我改变调用
<Button title="<20" onPress={(e)=>console.log(this.data) }/>
我得到的日志,与有效值。
为什么'this.data'超出范围?
是什么'this.data'?它是您的react-native组件中的静态属性吗? –
这是一个“Immutable.js”状态数据对象。向问题添加了示例代码。 –
这个在javascript中的概念有点不同。直接传递onApproximateAgePress方法可能会改变此上下文。我鼓励你看看这个:https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work –