如何在React Native中綁定超出範圍的函數?我發現了錯誤:未定義不是評估this.state/this.props的對象
undefined is not an object evaluating this.state
&
undefined is not an object evaluating this.props
我使用的渲染方法時的數據加載勾起renderGPSDataFromServer()
。問題是,我試圖在renderGPSDataFromServer()
裏面使用_buttonPress()
和calcRow()
,但是我收到了這些錯誤。
我添加
constructor(props) {
super(props);
this._buttonPress = this._buttonPress.bind(this);
this.calcRow = this.calcRow.bind(this);
我的構造,我已經改變了_buttonPress() {
到_buttonPress =() => {
,仍然一無所獲。
我想我明白這個問題,但我不知道如何解決它:
renderLoadingView() {
return (
<View style={[styles.cardContainer, styles.loading]}>
<Text style={styles.restData}>
Loading ...
</Text>
</View>
)
}
_buttonPress =() => {
this.props.navigator.push({
id: 'Main'
})
}
renderGPSDataFromServer =() => {
const {loaded} = this.state;
const {state} = this.state;
return this.state.dataArr.map(function(data, i){
return(
<View style={[styles.cardContainer, styles.modularBorder, styles.basePadding]} key={i}>
<View style={styles.cardContentLeft}>
<TouchableHighlight style={styles.button}
onPress={this._buttonPress().bind(this)}>
<Text style={styles.restData}>View Video</Text>
</TouchableHighlight>
</View>
<View style={styles.cardContentRight}>
<Text style={styles.restData}>{i}</Text>
<View style={styles.gpsDataContainer}>
<Text style={styles.gpsData}>
{Number(data.lat).toFixed(2)}</Text>
<Text style={styles.gpsData}>{Number(data.long).toFixed(2)}</Text>
</View>
<Text style={styles.gpsData}>
{this.calcRow(55,55).bind(this)}
</Text>
</View>
</View>
);
});
}
render =()=> {
if (!this.state.loaded) {
return this.renderLoadingView();
}
return(
<View>
{this.renderGPSDataFromServer()}
</View>
)
}};
如何去修復這一點,在這種情況下,是什麼問題?
我也認爲你應該遵循皮內達的建議,關於如何處理我完全忽略了我的答案的道具。 – martinarroyo