react-native - 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}/>
</>)
}
}
解决方案
推荐阅读
- windows - 线程本地存储和线程变量
- assembly - 在 x86 NASM 中向字符串添加分号?
- vue.js - Vuex:为什么我的突变提交调用失败?
- python - 异步 - 等待 Discord.py 函数问题
- jquery - 在 jqGrid 中使用外键的下拉菜单进行编辑
- c# - 为什么我无法在 Skype 频道的 Azure 表中保存和检索机器人数据?
- python - 用 python 编写 Xlsx 文件 - Excel 文件上的空白行
- r - 字符串的组合
- python-3.x - how to input in multiple lines in Python 3.7
- tfs - 将待办事项与功能相关联