2017-02-24 116 views
0

我有一个列表视图,显示通过Json的数据。我介绍了一个函数,并在每行上单击时显示一条警报,其中包括{shop_name}。 但是,起初它不认识功能。 功能是在同一个班 当我把警报在onPress,它的工作原理未定义不是一个对象(评估'this.onSubmitPressed.bind')

class SearchPage extends Component { 

     constructor(props) { 
      super(props); 
      var dataSource = new ListView.DataSource({rowHasChanged:(r1,r2) => r1.ruid != r2.guid}); 
      this.state = { 
      id: 'SearchPage', 
      shopid: this.props.shopid, 
      homesearchinpt: this.props.homesearchinpt, 
      dataSource: dataSource.cloneWithRows(shopsArray), 
      isLoading: true 
      }; 
     } 

     componentDidMount(){ 
      this.fetchData(); 
     } 
     onSubmitPressed(){ 
      Alert.alert(
      'Alert Title', 
      'alertMessage', 
     ) 
     } 
     fetchData() { 
      fetch(REQUEST_URL) 
       .then((response) => response.json()) 
       .then((responseData) => { 
        this.setState({ 
        dataSource: this.state.dataSource.cloneWithRows(responseData), 
        loaded: true, 
       }); 
      }) 
      .done(); 
     } 
     render() { 
      if (!this.state.loaded) { 
       return this.renderLoadingView(); 
      } 
      return (
       <ListView 
        dataSource={this.state.dataSource} 
        renderRow={this.renderShops} 
        style={styles.listView} 

       /> 
      ); 
     } 
     renderLoadingView() { 
      return (
       <View style={styles.container}> 
        <Text>Loading</Text> 
       </View> 
      ); 
     } 

     renderShops(shop) { 

      return (


       <View style={styles.container} > 
      <TouchableOpacity onPress={this.onSubmitPressed.bind(this)}> 
        <Image 
       source={{uri: shop.shop_logo}} 
       style={{width: 50, height: 50}}> 
      </Image> 
       <View> 
       <Text style={styles.shop_name}>{shop.shop_name}</Text> 
       <Text style={styles.shop_description}>{shop.shop_description}</Text> 
       </View> 

      </TouchableOpacity> 
      </View> 

      ); 
     } 

}; 

错误:

undefined is not an object(evaluating 'this.onSubmitPressed.bind')

+0

考虑在构造函数中对你的'funcs'进行'bind' – jose920405

回答

0
onSubmitPressed(shopName) { 
    Alert.alert(
    'Alert Title', 
    shopName, 
) 
} 

<TouchableOpacity onPress={() => this.onSubmitPressed(shop.shop_name)}> 
+0

即使没有shop.shop_name,我仍然有同样的错误。只有基本的警报也有错误。它找不到功能:( –

0

我重写了代码和问题解决了。 谢谢

相关问题