javascript - 在 fetch 开始 React Native 后制作不可点击的按钮
问题描述
我试图在获取开始后禁用我的按钮,这样他们就不能进行多次获取,当获取完成以使其进程使按钮再次可点击时,你知道如何实现吗?
这是我的代码:
import React from 'react';
import { StyleSheet, Text, View, Button, ActivityIndicator } from 'react-native';
export default function App() {
const [nombre,setNombre]= React.useState('');
const [isLoading,setIsLoading]= React.useState(false);
const fetchDatos = async () => {
setIsLoading(true);
return fetch('http://localhost:8000/api/consulta', {method: 'POST', headers: new Headers({'Accept': 'application/json',
'Content-Type': 'application/json',
}), body: JSON.stringify({
codigoParticipante: '1520',
contrato: '135927',
})}).then(response => {
return response.json();
})
.then(responseJson => {
setIsLoading(false);
setNombre(responseJson.Participante.InfoParticipante['@attributes'].Nombre);
}}).catch(function() {
alert("Sorry, server offline");
});
}
return (
<View>
<Button
title='press me'
onPress={fetchDatos}
/>
<Text>{nombre}</Text>
{isLoading && (
<ActivityIndicator
style={[{height: 80}]}
color="#C00"
size="large"
hidesWhenStopped={true}
/>
)}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
任何建议我都会非常感激!
解决方案
获取时禁用按钮
<Button
title='press me'
onPress={fetchDatos}
disabled = {isLoading}
/>
推荐阅读
- swiftui - 如何在 iPad 上显示与 iPhone 相同的视图而不是拆分视图?
- entity-framework - EF核心多对多关系违反主键约束实体
- python - 从文件读取连接 Bags 时出现意外行为
- sql - 如何在 PL/SQL 中将每日快照转换为有效日期范围?
- c - 为什么可以在 scanf 的转换说明符中嵌入空字符?
- python - 除法运算符重载参数具有相同的值
- image-processing - 在网络摄像头上部署 pytorch 模型
- mysql - 求和到某个特定值后,如何在 MySQL 中从上到下跳过和检索所有行?
- package - 如何确定自制程序包是否针对苹果 M1 进行了优化
- javascript - 我想在 postgress 中插入一个数组类型的数组,但我不知道如何从节点执行它