数组不是一个函数(评估'this.props.onPress(e)')

时间:2018-12-01 11:10:05

标签: javascript reactjs react-native button render

这是错误屏幕: The ERROR SCREEN

渲染代码中的按钮:

<View style={{flex:1}}>
  <Image source={require('../images/plots2.jpg')}
    style={{flex:1,height:null,width:"100%",resizeMode:'contain'}}
  />
  <Button onPress = {this.abc(this.state.items)}
    title="Retrive Database Table"
  />
</View>

按钮中的功能在此处称为代码:

abc (items) {
  console.log("IN hashir");
  //console.log("Hashir RAN count: "+this.state.countrunHashir);
  //var a=this.state.countrunHashir+1;
  //this.setState(countrunHashir:1);
  //this.state.countrunHashir+=1;

  return(
    items.map(item=>{
      return(
        <Card style={{marginLeft: 5, marginRight:5}}>
          <CardItem header style={{borderBottomWidth:1,borderBottomColor:'#dee0e2'}}>
            <Text style={{backgroundColor:'white',color:'red',fontSize:25,fontWeight:'bold',textAlign:'center',justifyContent:'center'}}>
              Items Recommended For You
            </Text>
          </CardItem>

          <RecommendedCardItem 
            title={item.Title}
            model="has"
            StartingBid="hashir"
            imageUri={require("../images/h1.jpg")}
            rating={3}
          />
          <TouchableOpacity style={{alignContent:'center', backgroundColor:'#b2ff59',width:'100%',borderRadius:25,marginVertical:10,paddingVertical:12 }}>
            <Text style={{fontSize:22, fontWeight:'bold', color:'#ffffff',textAlign:'center',color:'black'}}>Click For More Details</Text>
          </TouchableOpacity>
        </Card>
      );
    })
  );
}

在构造函数代码中初始化的状态:

this.state={
  items:[],
  mainscreenrender:0,
  countrunHashir:0
};

渲染问题是当按钮希望使用数组“ items”渲染整个视图列表时。您能为我改善代码吗? 非常感谢您。

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试为您的onPress属性(而不是“ value”)提供回调函数。您的代码应如下所示:

<Button onPress = {() => this.abc(this.state.items)} 
        title="Retrive Database Table" />