按钮不响应异步反应本机的问题



我从github项目中复制了流浪代码,并尝试使用expo。该项目没有错误执行,但是当我按下按钮时,什么也不会发生。甚至没有错误,这是我的代码NB-我在OnChooseImagePress内部发出警报,并且警报正常工作

import React from 'react';
import { Image, StyleSheet, Button, Text, View, Alert, } from 'react-native';
import { ImagePicker } from 'expo';
import * as firebase from 'firebase';
import {firebaseConfig} from "./ApiKeys";
export default class HomeScreen extends React.Component {
    static navigationOptions = {
        header: null,
    };
    onChooseImagePress = async () => {
        let result = await ImagePicker.launchCameraAsync();
        //let result = await ImagePicker.launchImageLibraryAsync();
        if (!result.cancelled) {
            this.uploadImage(result.uri, "test-image")
            .then(() => {
                Alert.alert("Success");
            })
            .catch((error) => {
                Alert.alert(error);
            });
        }
    }
    uploadImage = async (uri, imageName) => {
        const response = await fetch(uri);
        const blob = await response.blob();
        var ref = firebase.storage().ref().child("images/" + imageName);
        return ref.put(blob);
    }
    render() {
        return (
            <View style={styles.container}>
            <Button title="Choose image..." onPress={this.onChooseImagePress} />
            </View>
            );
        }
    }
    const styles = StyleSheet.create({
        container: { flex: 1, paddingTop: 50, alignItems: "center", },
    });
}

代码中的多个句法问题:

  1. const样式... 应在渲染函数中定义,目前其在类外悬在类
  2. 之外
  3. 支架不匹配
        return (
            <View style={styles.container}>
            <Button title="Choose image..." onPress={this.onChooseImagePress} />
            </View>
            );
        }
    } // the class ends here

Please let me know if it still doesn't work

尝试使用以下代码

constructor() {
    super();
    this.state = { }; 
    this.onChooseImagePress= this.onChooseImagePress.bind(this);
  } 
<Button title="Choose image..." onPress={() => this.onChooseImagePress()} /> 

相关内容

  • 没有找到相关文章

最新更新