简体   繁体   中英

React Native, Updating user profile image

I'm trying to update user profile pic by using Image picker but when I select an Image to update it, it's not updating and just and default image also get removed. I'm really getting a lot of problems by using Image picker with class base, please someone help

Here is my code

class Profile extends Component {
  state = {
    userImage: require("../assets/cena.jpg"),
    userName: "John Cena",
    userInfo: "The champ is here",
    modal: false,
    image: null,
    hasCameraPermission: null,
  };

  async componentDidMount() {
    const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
    this.setState({ hasCameraPermission: status === "granted" });
  }

  pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.Images,
      allowsEditing: true,
      aspect: [4, 3],
    });

    if (!result.cancelled) {
      this.setState({ image: result.uri });
    }
  };

  closeModal = () => {
    this.setState({ modal: false });
  };

  render() {
    return (
      <SafeAreaView style={styles.screen}>
        <View style={styles.container}>
          <TouchableOpacity
            activeOpacity={0.7}
            onPress={this.pickImage}
            style={styles.TouchableOpacityStyle}
          >
            <MaterialCommunityIcons
              name="camera"
              size={30}
              color={colors.white}
            />
          </TouchableOpacity>
          {!this.state.image && (
            <Image style={styles.image} source={this.state.userImage} />
          )}
          {this.state.image && (
            <Image
              style={styles.image}
              source={{ uri: this.setState.userImage }}
            />
          )}

        

你有一个错字, source={this.state.userImage}而不是 source={this.setState...} 而你正在设置的状态是image ,所以它应该是this.state.image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM