我是创建应用程序的第一步,我想在获取数据时在屏幕上实现加载微调器。当用户键入其详细信息以登录到应用程序时,会在signUp / signIn屏幕上出现此问题。当他按登录时,我要运行加载微调器,直到计时器结束。 请检查下面的代码,当加载出现在屏幕上大约一秒钟,然后继续进入下一个屏幕,尽管我将计时器设置为30000 ms:
import { Text, View, ActivityIndicator} from 'react-native';
const LoginScreen = ({ navigation }) => {
const [isLoading, setIsLoading] = useState(false);
const _onLoginPressed = () => {
setIsLoading(true);
const user = {
Email: email.value,
PasswordTourist: password.value
};
fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(user),
headers: new Headers({
'Content-type': 'application/json; charset=UTF-8'
})
})
.then(res => {
return res.json()
})
.then(
(result) => {
closeActivityIndicator();
const profile = result;
if(profile.Email === null || profile.PasswordTourist === null){
...code....
setIsLoading(false)
}
else{
navigation.navigate('Dashboard', { profile: profile })
}
},
(error) => {
console.warn("err post=", error);
});
}
};
const closeActivityIndicator = async => setTimeout(() => {setIsLoading(false)}, 30000)
return (
<Background>
code......
{isLoading && <ActivityIndicator size="large" color="#fff" />}
code....
</Background>
)