简体   繁体   中英

How to get user id from firebase in react native?

I need your help I'm using firebase for my app. I'm trying to get the users ID not the logged users no all users I have. I want to show their (uid) simply like in an alert for each user. Also, I'm showing them in a flatlist and when I set item.uid in an alert it shows (undefined) . But, all the other data of the user is shown correctly. This what I did until now:

** users.js **

export default class usersList extends React.Component{

  state = {
    loading: false,
    uid: '',
    users: [],
    items: []
   };

   componentDidMount() {
        let itemsRef = f.database().ref('users').once('value').then(snapshot => {
               var data = snapshot.val();
               var items = Object.values(data);
               this.setState({items});
               console.log(snapshot.val())

            });
       }


  renderItem({item}) {
    return (
       <View key={index} style={{width: '100%', overflow:'hidden', marginBottom: 5, justifyContent:'space-between', borderBottomWidth:1, borderColor: 'grey'}}>
          <View>
          <View style={{padding:5, width:'100%', flexDirection: 'row', justifyContent: 'space-between'}}>

              <Text>{item.email}</Text>

          </View>
          </View>
        </View>
      )
  }

  render() {
   return (

     <View style={styles.container}>
     <ScrollView>
                {
                    this.state.items.length > 0
                    ? <ItemComponent items={this.state.items} navigation={this.props.navigation} />
                    : <Text>No stores</Text>
                }
                </ScrollView>
            </View>

  );
 }
 }

//ItemComponent.js

export default class ItemComponent extends Component {

  static propTypes = {
      items: PropTypes.array.isRequired
  };

  render() {
    return (
      <View style={styles.itemsList}>
        {this.props.items.map((item, index) => {
            return (
                <View key={index}>
                <TouchableOpacity
                onPress={ () => alert(item.uid)}>
                    <Text style={styles.itemtext}>{item.email}</Text>
                    </TouchableOpacity>
                </View>
            )
        })}
      </View>
    );
  }
}
firebase.database().ref('user').on('value', (datasnapshot) => {
    this.setState({
       _id: datasnapshot.key
     });`

这个解决方案对我有用

<Text style={styles.yourStyleHere}>UID: {auth.currentUser?.uid} </Text>

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