我的目标是使用react-native-camera,只需在同一屏幕上显示图片,如果拍摄了照片.我正在尝试将图片源保存为"imageURI".如果它存在,我想展示它,如果还没有拍摄照片,只显示没有图像的文字.我有相机工作,因为我可以跟踪应用程序将图片保存到磁盘.遇到以下问题:
不知道如何在Javascript中执行if语句来检查变量是否存在.
import Camera from 'react-native-camera';
export default class camerahere extends Component {
_takePicture () {
this.camera.capture((err, data) => {
if (err) return;
imageURI = data;
});
}
render() {
if ( typeof imageURI == undefined) {
image = <Text> No Image Yet </Text>
} else {
image = <Image source={{uri: imageURI, isStatic:true}}
style={{width: 100, height: 100}} />
}
return (
<View style={styles.container}>
<Camera
captureTarget={Camera.constants.CaptureTarget.disk}
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
aspect={Camera.constants.Aspect.fill}>
{button}
<TouchableHighlight onPress={this._takePicture.bind(this)}>
<View style={{height:50,width:50,backgroundColor:"pink"}}></View> …Run Code Online (Sandbox Code Playgroud)我有一个正在运行的用户登录功能.但是,我想为fetch合并一个超时错误.有没有办法设置一个5秒左右的计时器,以便在这样的时间后停止尝试获取?否则,我只是在网络错误一段时间后得到一个红色屏幕.
_userLogin() {
var value = this.refs.form.getValue();
if (value) { // if validation fails, value will be null
if (!this.validateEmail(value.email)) {
Alert.alert(
"Enter a valid email",
)
} else {
fetch("http://51.64.34.134:5000/api/login", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
timeout: 5000,
body: JSON.stringify({
username: value.email,
password: value.password,
})
})
.then((response) => response.json())
.then((responseData) => {
if (responseData.status == "success") {
this._onValueChange(STORAGE_KEY, responseData.data.token)
Alert.alert(
"Login Success!",
)
this.props.navigator.push({ name: 'StartScreen'})
} else if (responseData.status == "error") {
Alert.alert(
"Login …Run Code Online (Sandbox Code Playgroud)