首页 > 解决方案 > onPress 没有读取处理函数

问题描述

我正在尝试将两件事传递给我的新闻功能。第一部分将状态设置为被单击元素的 id,而我将需要第二个功能(POST 请求)。设置状态是成功的,但 POST 请求甚至从未被命中。我需要将 setState 部分保留在 onPress 中,因为它是我唯一可以访问被单击项目的地方。任何想法为什么第二个功能没有被击中?任何解决方法?我对原生反应比较陌生,所以任何想法都会非常有帮助。谢谢!

handlePress=()=> {
    debugger
    fetch('http://localhost:3000/user_resources', {
          method: 'POST',
          headers: {
              'Content-Type': 'application/json',
              Accept: 'application/json'
          },
          body: JSON.stringify({
              resource_id: this.state.eventId,
              user_id: this.props.users.id
          })
      })
          .then(res => res.json())
          .then(data2 => {
              this.props.addUserResource(data2)
          })
  }

  header = () => {
  return <Text style={styles.header}>Hello, {this.props.users.name}</Text>
  }
 
  render(){
    console.log(this.state)
     return(
       <>
      <Button title="Add A New Resource" onPress={() => this.props.navigation.navigate('Add A New Resource',{topicId:this.props.route.params.topicId})} style={styles.listitem} />
      <FlatList style={styles.flatlist} keyExtractor={(item)=> item.id.toString()} data={anoda} ItemSeparatorComponent = { this.FlatListItemSeparator } renderItem={({item}) => {
       return <TouchableOpacity><Text onPress={() => this.props.navigation.navigate('Add A New Resource',{topicId:item.id})} style={styles.listitem}>{item.name}</Text><Button title="worku?" onPress={()=> {this.setState({eventId:item.id})}}/></TouchableOpacity>
      }
    }
      ListHeaderComponent = {this.header}/>
     </>)
   }
}

标签: react-nativepostevent-handlingreact-native-flatlistonpress

解决方案


推荐阅读