【发布时间】:2017-04-08 01:10:43
【问题描述】:
如何在 React Native 中绑定超出范围的函数?我收到了错误:
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>
)
}};
我该如何解决这个问题?在这种情况下,问题是什么?
【问题讨论】:
标签: javascript reactjs scope react-native jsx