React native 如何在react native中的变量中存储函数的返回值

React native 如何在react native中的变量中存储函数的返回值,react-native,React Native,我创建了两个函数,一个是img(item),另一个是async get_customize_category_image(id),我在img()函数中调用了第二个函数,我想在img变量中传递第二个函数的返回值,但在img()中调用了console.log()函数未接收到正确的值img()函数的日志正在返回{“_40”:0,“_65”:0,“_55:null,“_72:null}此数据。如何在img()函数中获得相同的值 请帮我解决这个问题 旧代码: async get_customize_ca

我创建了两个函数,一个是img(item),另一个是async get_customize_category_image(id),我在img()函数中调用了第二个函数,我想在img变量中传递第二个函数的返回值,但在img()中调用了console.log()函数未接收到正确的值img()函数的日志正在返回{“_40”:0,“_65”:0,“_55:null,“_72:null}此数据。如何在img()函数中获得相同的值

请帮我解决这个问题

旧代码:

 async get_customize_category_image(id) {
    const response = await fetch(
      'http://192.168.0.3:1234/get_customize_category_image?id=' + id,
    );
    const data = await response.json();
    let img = data;
    console.log(img);
    return img;
  }

  img(item) {
    if (item.customize_category) {
      let img = this.get_customize_category_image(item.customize_category);
      console.log(img);
      return (
        <Image style={styles.profilePic} source={{uri: img}} />
      );
    } else {
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    }
  }
    async get_customize_category_image(id) {
    const response = await fetch(
      'http://192.168.0.3:1234/get_customize_category_image?id=' + id,
    );
    const data = await response.json();
    let img = data;
    // console.log(img);
    return String(img[0].image);
  }

  async img(item) {
    if (item.customize_category) {
      let img = await this.get_customize_category_image(
        item.customize_category,
      ).catch(err => {
        console.log(err);
      });
      console.log(img);
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    } else {
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    }
  }

  render() {
    let img = '';
    return (
      <View style={styles.container}>
        <HeaderIcon />
        {this.state.loading && <ActivityIndicator size="large" color="cyan" />}

        <FlatList
          onScroll={({nativeEvent}) => {
            if (this.isCloseToBottom(nativeEvent)) {
              this.getMorePost();
            }
          }}
          data={this.state.post}
          keyExtractor={(item, index) => index.toString()}
          renderItem={({item}) => (
            <TouchableOpacity
              style={styles.main}
              onPress={() => {
                this.openPost(item);
              }}>
              <View style={styles.row}>
                <View>{this.img(item)}</View> // Hear I'm calling my img() function

                <View>
                  <Text style={styles.title}>{item.post_title}</Text>
                </View>
              </View>
              <Image
                resizeMode="stretch"
                style={styles.image}
                source={{uri: item.featuredImage}}
              />
              <View>
                <TouchableOpacity
                  style={{justifyContent: 'center', padding: 10}}
                  onPress={() => {
                    Share.share({
                      title: item.post_title,
                      message: item.post_content.replace(
                        /<[^>]*>|&nbsp;/g,
                        ' ',
                      ),
                      uri: item.featuredImage,
                    });
                  }}>
                  <Image
                    source={require('../image/wlogo.png')}
                    style={{height: 45, width: 45, paddingLeft: 60}}
                  />
                </TouchableOpacity>
              </View>
            </TouchableOpacity>
          )}
        />
      </View>
    );
  }
}
在您的img(项目)中,将代码替换为:

img =async(item) => {
    if (item.customize_category) {
      let img = await this.get_customize_category_image(item.customize_category);
      console.log(img);
      return (
        <Image style={styles.profilePic} source={{uri: img}} />
      );
    } else {
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    }
  }
img=async(项目)=>{
if(项目自定义\类别){
让img=wait.get_customize_category_图像(item.customize_category);
控制台日志(img);
返回(
);
}否则{
返回(
);
}
}

希望ITE能帮助您获得回程承诺,这样就可以拒绝或解决回程承诺,您可以使用下面的then和catch访问数据。您需要了解回程的更多基础知识

    async get_customize_category_image(id) {
    let IMAGES = [];

   await fetch(
      'http://192.168.0.3:1234/get_customize_category_image?id=' + id,
    ).then(response => {
         IMAGES = response.json();
    }).catch(err=>{
      console.log(err)
    });

    console.log(IMAGES)

    return IMAGES[0].image;

  }

  async img(item) {
    if (item.customize_category) {
      let img = await this.get_customize_category_image(item.customize_category)
      console.log(img);
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    } else {
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    }
  }
异步获取\自定义\类别\映像(id){
让图像=[];
待命(
'http://192.168.0.3:1234/get_customize_category_image?id=“+id,
)。然后(响应=>{
IMAGES=response.json();
}).catch(错误=>{
console.log(错误)
});
console.log(图像)
返回图像[0]。图像;
}
异步img(项目){
if(项目自定义\类别){
让img=等待。获取\自定义\类别\图像(item.customize\类别)
控制台日志(img);
返回(
);
}否则{
返回(
);
}
}

谢谢,但有点问题,我用新代码和新错误编辑了我的问题,请帮我解决..1分钟我正在更新我的答案
    async get_customize_category_image(id) {
    let IMAGES = [];

   await fetch(
      'http://192.168.0.3:1234/get_customize_category_image?id=' + id,
    ).then(response => {
         IMAGES = response.json();
    }).catch(err=>{
      console.log(err)
    });

    console.log(IMAGES)

    return IMAGES[0].image;

  }

  async img(item) {
    if (item.customize_category) {
      let img = await this.get_customize_category_image(item.customize_category)
      console.log(img);
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    } else {
      return (
        <Image style={styles.profilePic} source={{uri: item.featuredImage}} />
      );
    }
  }