【问题标题】:Getting an Image uri into render from an async Function从异步函数获取图像 uri 渲染
【发布时间】:2019-09-19 15:16:24
【问题描述】:

有点麻烦。我应该首先说我对原生反应很陌生,所以希望这不是我错过的愚蠢行为。我正在使用 firebase 来存储图像 uri,但是当我尝试从我的函数中将它们传递给 render 时,它给了我 {_40:0,_65:0,_55:null,_72:null} 输出但在函数中,似乎得到 uri 就好了,所以它似乎是把它传回去的东西。

我尝试过使用 AsyncImage(使用太阳镜狗示例:https://www.npmjs.com/package/react-native-async-image-animated),但我认为它只是永远加载,渲染中的输出保持不变。


export class PinImgScreen extends React.Component {
  render() {
    return (
      <View
        style={{
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
          backgroundColor: 'white',
        }}>
        {console.log(get_firebaseImgTest(targetinMem, 1))}

        <AsyncImage
          style={{
            borderRadius: 50,
            height: 100,
            width: 100,
          }}
          source={{
            uri: get_firebaseImgTest(targetinMem, 1),
          }}
          placeholderColor="#b3e5fc"
        />
      </View>
    );
  }
}


function get_firebaseImgTest(userId, num) {
  return new Promise(function(resolve, reject) {
    setTimeout(() => {
      firebase
        .database()
        .ref('Info1/' + userId + '/imgInfo' + num)
        .on('value', snapshot => {
          console.log('GIBS ' + snapshot.val().imgInfo);
          resolve(snapshot.val().imgInfo);
          reject('https://www.gstatic.com/webp/gallery/1.jpg');
        });
    }, 200);
  });
}

输出:

1: {_40:0,_65:0,_55:null,_72:null}

2:GIBShttps://www.gstatic.com/webp/gallery/2.jpg

【问题讨论】:

    标签: firebase react-native firebase-realtime-database async-await render


    【解决方案1】:

    您遇到这个问题是因为 get_firebaseImgTest() 返回的是一个承诺,而不是一个字符串。

    解决此问题的一种方法是将图像 URI 存储在组件的状态中,并通过 componentDidMount 回调对其进行更新,如下所示:

    export class PinImgScreen extends React.Component {
    
        constructor(props) {
            super(props);
            this.state = {
                imageUri: 'https://www.gstatic.com/webp/gallery/1.jpg'
            }
        }
    
        componentDidMount() {
            get_firebaseImgTest(targetinMem, 1)
                .then(imageUri => this.setState({imageUri}))
                .catch(imageUri => this.setState({imageUri}))
        }
    
        render() {
            return (
                <View
                    style={{
                        flex: 1,
                        justifyContent: 'center',
                        alignItems: 'center',
                        backgroundColor: 'white',
                    }}>
                    {console.log(get_firebaseImgTest(targetinMem, 1))}
    
                    <AsyncImage
                        style={{
                            borderRadius: 50,
                            height: 100,
                            width: 100,
                        }}
                        source={{
                            uri: this.state.imageUri
                        }}
                        placeholderColor="#b3e5fc"
                    />
                </View>
            );
        }
    }
    

    【讨论】:

    • 您好,朋友感谢您的回复。它说 imageUri 是空的“TypeError:null 不是对象(评估'this.state.imageUri')”。我返回了一个字符串 uri,但也没有运气。
    • 如果您还没有构造函数,您将需要一个构造函数来初始化您的状态。请参阅我的编辑以了解实现
    • 工作。谢谢!!
    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 2017-10-28
    • 2018-12-22
    • 2021-03-15
    • 2016-08-02
    相关资源
    最近更新 更多