从Firestore提取数据后,无法在平面列表中显示数据

时间:2020-06-10 14:57:08

标签: javascript firebase react-native visual-studio-code google-cloud-firestore

我的Firestore中有一些数据。结构是:

User -> DocId(Some id)-> name(String), number(String), friends (array)

我得到了friends数组,并追加到本地数组数据中。现在,当我在平面列表中进行渲染时。它没有显示。但是我在本地创建的相同临时数据工作正常。从firestore中获取数据后,我会检查阵列数据计数。它显示正确的计数。但是无法在平面列表中显示数据。

我的代码:

  import firebase from '@react-native-firebase/app';
    import firestore from '@react-native-firebase/firestore';

    UserArray =[]

    export default class HomeScreen extends React.Component {


 constructor(props) {
        super(props);
        this.state = {
            UserArray: [],
        }
      }
        componentWillMount() {

            firebase.firestore().doc(`User/${'beQVEcfLsXV8JhA8L2SDDq03bmD3'}`)
             .get()
             .then(doc => {
                this.setState({UserArray: doc.data().friends})
           })
         }

        renderPost = post => {
            return (
                <View style={styles.feedItem}>
                    <Feather  name="book" style={styles.avatar}  size={30}/>
                    <View style={{ flex: 1 }}>
                        <View style={{ flexDirection: "row", justifyContent: "space-between", alignItems: "center" }}>
                            <View>
                                <Text style={styles.name}>{post.name}</Text>
                                  <Text style={styles.timestamp}>{post.name}  |  {post.name}</Text>
                            </View>
                        </View>
                    </View>
                    <Feather name="chevron-right" style={{color: "#808080", alignSelf: 'center'}} size={20}/>
                </View>
            );
        };

        render() {
            return (
                <View style={styles.container}>
                    {
                        UserArray.length ?
                        (<FlatList
                            style={styles.feed}
                            data={UserArray}
                            keyExtractor={(index) => index.toString()}
                            renderItem={({ item }) => this.renderPost(item)}
                            // keyExtractor={item => item.id}
                            showsVerticalScrollIndicator={false}
                        ></FlatList>) :
                        (
                 <View style={{  width: '70%', alignSelf: 'center',justifyContent: 'center' , alignItems: 'center', marginTop: '20%'}}>
                    <Feather name="smile" color="#009387" size={40}/>
                    <Text style={{paddingTop: 20, textAlign: 'center', fontSize: 15, fontWeight: 'bold', color: '#A9A9A9'}}>Hey folk, You dont' have any friends list</Text>
                </View>
                        )
                    }

                </View>
            );
        }
    }

在我的屏幕上,我总是得到。您没有任何数据。

但是在componentDidMount()中,我从Firestore中获取了数据。在我的那时:

.then(doc => {
alert(UserArray.length);
})

它显示正确的数组计数。这是我的朋友数组:

{
  "name": "quaroe",
"number": "3940904",
friends: [
     {
       "name": "alber"
     },
     {
       "name": "romea"
     },
     {
       "name": "basea"
     }
]
}

我的疑问是,rendercomponentDidMount之前被调用。但是我尝试了componentWillMount。而且我添加了警报。总是先调用render,然后再调用componentWillMount or componentDidMount

1 个答案:

答案 0 :(得分:1)

将您的UseArray置于状态中。我相信该平面列表不会显示任何内容,因为渲染时您的firebase函数尚未完成,因此UseArray为空。而且render无法检测到您的常量UseArray的变化,这就是为什么即使您获取成功也不会显示任何列表的原因。

尝试

import firestore from '@react-native-firebase/firestore';

export default class HomeScreen extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      userArray: [],
    };
  }

  componentDidMount() {
    firebase
      .firestore()
      .doc(`User/${'beQVEcfLsXV8JhA8L2SDDq03bmD3'}`)
      .get()
      .then(doc => {
        this.setState({userArray: doc.data().friends});
      });
  }

  renderPost = post => {
    return (
      <View style={styles.feedItem}>
        <Feather name="book" style={styles.avatar} size={30} />
        <View style={{flex: 1}}>
          <View
            style={{
              flexDirection: 'row',
              justifyContent: 'space-between',
              alignItems: 'center',
            }}>
            <View>
              <Text style={styles.name}>{post.name}</Text>
              <Text style={styles.timestamp}>
                {post.name} | {post.name}
              </Text>
            </View>
          </View>
        </View>
        <Feather
          name="chevron-right"
          style={{color: '#808080', alignSelf: 'center'}}
          size={20}
        />
      </View>
    );
  };

  render() {
    return (
      <View style={styles.container}>
        {this.state.userArray && this.state.userArray.length > 0 ? (
          <FlatList
            style={styles.feed}
            data={this.state.userArray}
            keyExtractor={index => index.toString()}
            renderItem={({item}) => this.renderPost(item)}
            // keyExtractor={item => item.id}
            showsVerticalScrollIndicator={false}
          />
        ) : (
          <View
            style={{
              width: '70%',
              alignSelf: 'center',
              justifyContent: 'center',
              alignItems: 'center',
              marginTop: '20%',
            }}>
            <Feather name="smile" color="#009387" size={40} />
            <Text
              style={{
                paddingTop: 20,
                textAlign: 'center',
                fontSize: 15,
                fontWeight: 'bold',
                color: '#A9A9A9',
              }}>
              Hey folk, You dont' have any friends list
            </Text>
          </View>
        )}
      </View>
    );
  }
}