how can i get data from firebase in descending order - reactjs

//this is my accounts.js where i am fetching accounts from user node in user node and pass to the card.js component where every account prints in cards with accounttitle and accountbalance . but when i take snapshot of one user's accounts they are fetched in ascending order but i want to get accounts from firebase in descending order how can i do this.this is my database structure in firebase
import React, { useState, useEffect } from "react";
import { Fontisto } from "#expo/vector-icons";
import Firebase from "../components/Firebase";
import { StyleSheet, Text, View, TouchableOpacity } from "react-native";
import Cards from "../components/Accounts/Cards";
import AddAccountModal from "../components/Accounts/AddAccountModal";
import FundTransferModal from "../components/Accounts/FundTransferModal";
export default function AccountsScreen() {
let add = {
id: 111,
title: "Add",
backgroundColor: "#fff",
};
let [total, setTotal] = useState(0);
let [accounts, setAccount] = useState([]);
const [backgroundColor, setbackgroundColor] = useState("#ff7f50");
const [addAccountmodal, SetAddAccountmodal] = useState(false);
const [fundModal, setFundModal] = useState(false);
const showAddmodal = () => {
SetAddAccountmodal(true);
};
const closeAddmodal = () => {
setbackgroundColor("#ff7f50");
SetAddAccountmodal(false);
};
const showFundModal = () => {
setFundModal(true);
};
const closeFundModal = () => {
setFundModal(false);
};
useEffect(() => {
let user = Firebase.auth().currentUser;
let uid = user.uid;
Firebase.database()
.ref("users/" + uid)
.child("accounts")
.on("value", (snapshot) => {
let tempAccounts = [];
let temptotal = 0;
snapshot.forEach(function (snapshotAccount) {
let account = {
id: snapshotAccount.key,
title: snapshotAccount.val().title,
balance: snapshotAccount.val().balance,
backgroundColor: snapshotAccount.val().backgroundColor,
};
let balance = parseFloat(account.balance, 10);
temptotal = temptotal + balance;
tempAccounts.push(account);
});
tempAccounts.push(add);
setTotal(temptotal);
setAccount(tempAccounts);
});
}, []);
return (
<View style={styles.container}>
<AddAccountModal
addAccountmodal={addAccountmodal}
closeAddmodal={closeAddmodal}
backgroundColor={backgroundColor}
setbackgroundColor={setbackgroundColor}
/>
<FundTransferModal
fundModal={fundModal}
closeFundModal={closeFundModal}
/>
<View style={styles.headingContent}>
<Text style={styles.heading}>Accounts</Text>
<View style={styles.balanceContent}>
<Text style={styles.heading}>Total: </Text>
<Text style={styles.balance}>+ Rs.{total}</Text>
<View style={styles.icon}>
{/* <TouchableOpacity onPress={() => showFundModal()}>
<Fontisto name="arrow-swap" size={25} color="#29416F" />
</TouchableOpacity> */}
</View>
</View>
</View>
<Cards accounts={accounts} showAddmodal={showAddmodal} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
headingContent: {
marginTop: 10,
paddingTop: 10,
paddingHorizontal: 21,
width: "100%",
height: 50,
flexDirection: "row",
justifyContent: "space-between",
},
balanceContent: {
flexDirection: "row",
},
heading: {
color: "#333",
fontWeight: "bold",
fontSize: 16,
},
balance: {
color: "green",
fontWeight: "bold",
fontSize: 16,
},
icon: {
flexDirection: "row",
justifyContent: "flex-end",
marginLeft: "5%",
justifyContent: "center",
},
});

We can use orderBy event to get data in asc or desc order.
For Example: citiesRef.orderBy("name", "desc"),
Reference: https://firebase.google.com/docs/firestore/query-data/order-limit-data

Related

How to make Toggle Button with Animated.View in React Native?

I can only use react native in the project, I need to make a Toggle Component with AnimatedView. I tried with react native switcher but it won't be responsive for mobile and web at the same time.
Here is my code
export const ToggleButton = () => {
const [isEnabled, setIsEnabled] = useState(false);
const [text, setText] = useState('');
const toggleSwitch = () => {
if (isEnabled) {
setText('OFF');
} else {
setText('ON');
}
setIsEnabled(previousState => !previousState);
};
return (
<View style={styles.container}>
<View>
{isEnabled ? <Text style={styles.textOn}>On</Text> : <Text style={styles.textOff}>Off</Text>}
<Switch
trackColor={{ false: Colors.BlueLight, true: Colors.PurpleLight }}
thumbColor={isEnabled ? Colors.BlueLight : Colors.BlueLight}
ios_backgroundColor="#3E3E3E"
onValueChange={toggleSwitch}
value={isEnabled}
/>
</View>
</View>
);
};
Someone give me a recommendation how to do it?
Hye finally i made a custom switch, do check out :
Do check out this expo https://snack.expo.dev/#gaurav1995/gnarly-sandwich
Its completely built with react native, no external libraries etc
Do lemme know in case of any doubts :)
import React, { useState, useRef } from 'react';
import {
Text,
View,
StyleSheet,
Animated,
TouchableOpacity,
Easing
} from 'react-native';
export default function App() {
const positionButton = useRef(new Animated.Value(0)).current;
const [isOn, setIsOn] = useState(false);
const startAnimToOff = () => {
Animated.timing(positionButton,{
toValue:0,
duration:500,
easing:Easing.ease
}).start()
};
const startAnimToOn = () => {
Animated.timing(positionButton,{
toValue:1,
duration:500,
easing:Easing.ease
}).start()
};
const positionInterPol = positionButton.interpolate({inputRange:[0,1],outputRange:[0,30]})
const backgroundColorAnim = positionButton.interpolate({inputRange:[0,1],outputRange:["#767577","#81b0ff"]})
const initialOpacityOn = positionButton.interpolate({inputRange:[0,1],outputRange:[0,1]})
const initialOpacityOff = positionButton.interpolate({inputRange:[0,1],outputRange:[1,0]})
const onPress = () => {
if (isOn) {
startAnimToOff();
setIsOn(false);
} else {
startAnimToOn();
setIsOn(true);
}
};
return (
<View style={styles.container}>
<TouchableOpacity style={{height:30,width:60}} activeOpacity={0.9} onPress={onPress} >
<Animated.View style={[styles.mainStyes,{
backgroundColor:backgroundColorAnim
}]} >
<Animated.Text
style={[
styles.eahcStyles,
{
opacity: initialOpacityOn,
},
]}>
ON
</Animated.Text>
<Animated.Text
style={[
styles.eahcStylesOf,
{
opacity: initialOpacityOff,
},
]}>
OFF
</Animated.Text>
<Animated.View style={[styles.basicStyle,{
transform:[{
translateX:positionInterPol
}]
}]} />
</Animated.View>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
basicStyle: {
height: 20,
width: 20,
borderRadius: 20,
backgroundColor: '#FFF',
marginTop: 5,
marginLeft: 5,
},
eahcStyles: {
fontSize: 14,
color: '#f5dd4b',
position: 'absolute',
top: 6,
left: 5,
},
eahcStylesOf: {
fontSize: 14,
color: '#f4f3f4',
position: 'absolute',
top: 6,
right: 5,
},
mainStyes: {
borderRadius: 30,
backgroundColor: '#81b0ff',
height: 30,
width: 60,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});

React Native Flatlist item navigate to new page

I'm trying to make an app where I can add item in a flatlist . Then each item I can navigate to a new page only for that specific item . In that page, I can add another flatlist.
To explain it more, lets say I add few Classroom item in the flatlist (Classroom A, Classroom B, Classroom C). When I press Clasroom A , it will navigate to a page named Classroom A . In that page, I can add and delete the name of each students using another flatlist.
How can I design the page for the Classsrooms ??? Because when I add the name of the student in classroom A, the names of the students is also available in the flatlist of other Classrooms.
This is my code for the Main Menu:
import React, { useState , useEffect } from 'react';
import {
View,
Text,
TouchableOpacity,
FlatList,
Alert,
TextInput,
StyleSheet,
} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import AsyncStorage from '#react-native-async-storage/async-storage';
import { useNavigation } from '#react-navigation/native';
export default function MainMenu(){
const [classroomInput, setClassroomInput] = useState('');
const [classroom, setClassroom] = useState([]);
const navigation = useNavigation();
useEffect(() => {
getClassroom();
}, []);
useEffect(() => {
saveClassroom(classroom);
}, [classroom]);
const saveClassroom = async (classroom) => {
try {
const stringifyClassroom = JSON.stringify(classroom);
await AsyncStorage.setItem('classroom', stringifyClassroom);
} catch (error) {
console.log(error);
}
};
const getClassroom = async () => {
try {
const classrooms = await AsyncStorage.getItem('classroom');
if (classrooms !== null) {
setClassroom(JSON.parse(classrooms));
}
} catch (error) {
console.log(error);
}
};
const addClassroom = () => {
if (classroomInput === ''){
Alert.alert('Error', 'Please input class');
} else {
const newClassroom = {
id: Math.random().toString(),
Classroom: classroomInput,
};
setClassroom([...classroom,newClassroom]);
setClassroomInput('');
}
};
const deleteClassroom = (classroomId) => {
const newClassrooms = classroom.filter(item => item.id !== classroomId);
setClassroom(newClassrooms);
};
return (
<View style={styles.container}>
<TextInput
style={styles.input}
placeholder={'Add Classrooms'}
value={classroomInput}
onChangeText={(text) => setClassroomInput(text)}
/>
<TouchableOpacity onPress={() => addClassroom()} style={styles.button}>
<Text>Add Classroom</Text>
</TouchableOpacity>
<FlatList
style={styles.flatlist}
data={classroom}
keyExtractor = { (item) => item.id.toString() }
renderItem={({ item }) => (
<TouchableOpacity onPress= {() => navigation.navigate('Classroom', item)} >
<View style={styles.listItem}>
<View>
<Text style={[styles.classText , {fontSize: 18}]}>
{item?.Classroom}
</Text>
</View>
<View >
<TouchableOpacity style={[styles.delete ]} onPress={() => deleteClassroom(classroom?.id)}>
<Icon name="remove" size={15} color={'#fff'} />
</TouchableOpacity>
</View>
</View>
</TouchableOpacity>
)}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
input: {
width: '70%',
borderBottomWidth: 1,
marginBottom: 20,
},
button: {
backgroundColor: 'lightblue',
padding: 10,
marginBottom: 10,
},
delete: {
backgroundColor: '#ff3333',
padding: 5,
color: '#fff',
borderWidth: 1,
borderColor: '#ff9999',
borderRadius: 5,
},
listItem: {
flexDirection: 'row',
justifyContent: 'space-between',
width: '70%',
alignItems: 'center',
},
});
And this is the code for the classroom:
/* eslint-disable prettier/prettier */
import React, { useState , useEffect } from 'react';
import {
View,
Text,
TouchableOpacity,
FlatList,
Alert,
TextInput,
StyleSheet,
} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
import AsyncStorage from '#react-native-async-storage/async-storage';
const Classroom = ( {navigation, route}) => {
const [studentInput, setStudentInput] = useState('');
const [student, setStudent] = useState([]);
useEffect(() => {
getStudent();
}, []);
useEffect(() => {
saveStudent(student);
}, [student]);
const saveStudent = async (student) => {
try {
const stringifyStudent = JSON.stringify(student);
await AsyncStorage.setItem('student', stringifyStudent);
} catch (error) {
console.log(error);
}
};
const getStudent = async () => {
try {
const students = await AsyncStorage.getItem('student');
if (students !== null) {
setStudent(JSON.parse(students));
}
} catch (error) {
console.log(error);
}
};
const addStudent = () => {
if (studentInput === ''){
Alert.alert('Error', 'Please input student name');
} else {
const newStudent = {
id: Math.random().toString(),
Name: studentInput,
};
setStudent([...student,newStudent]);
setStudentInput('');
}
};
const deleteStudent = (studentId) => {
const newStudent = student.filter(item => item.id !== studentId);
setStudent(newStudent);
};
return (
<View styles={styles.container}>
<TouchableOpacity onPress={()=> navigation.goBack()} style={styles.button}>
<Text>Back</Text>
</TouchableOpacity>
<Text style={{fontWeight: 'bold', fontSize: 20}}>[ Classroom Name ]</Text>
<TextInput
style={styles.input}
placeholder={'Add Student Name'}
value={studentInput}
onChangeText={(text) => setStudentInput(text)}
/>
<TouchableOpacity onPress={()=> addStudent()} style={styles.button}>
<Text>Add Student</Text>
</TouchableOpacity>
<FlatList
style={styles.flatlist}
data={student}
keyExtractor = { (item) => item.id.toString() }
renderItem={({ item }) => (
<View style={styles.listItem}>
<View>
<Text style={[styles.classText , {fontSize: 18}]}>
{item?.Name}
</Text>
</View>
<View >
<TouchableOpacity style={[styles.delete ]} onPress={() => deleteStudent(item?.id)}>
<Icon name="remove" size={15} color={'#fff'} />
</TouchableOpacity>
</View>
</View>
)}
/>
</View>
);
};
export default Classroom;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
input: {
width: '70%',
borderBottomWidth: 1,
marginBottom: 20,
},
button: {
backgroundColor: 'lightblue',
padding: 10,
marginBottom: 10,
},
listItem: {
flexDirection: 'row',
justifyContent: 'space-between',
width: '70%',
alignItems: 'center',
},
delete: {
backgroundColor: '#ff3333',
padding: 5,
color: '#fff',
borderWidth: 1,
borderColor: '#ff9999',
borderRadius: 5,
},
});
You are using the same AsyncStorage key 'student' regardless of which classroom you are looking at. You need to keep your student lists separate, so you'll need a separate key for each. Something like:
const key = `students_in_room_${classroomId}`
You can get the classroom id (A, B, C) through the route prop from your navigation.
Edit: You would use this unique key instead of the string 'students' for reading and writing to AsyncStorage. That way you can have separate stored values for each classroom.
const students = await AsyncStorage.getItem(key);
await AsyncStorage.setItem(key, stringifyStudent);

How to navigate to different screen without showing the screen in react native

I want to navigate from my login screen to the home screen without showing the view. I am trying to do is if from the splash screen if it gets the username from async storage then move to login page ...wherein login page by if the username matches from the POST method in login screen directly login if not then show the login screen view
here is my code
import React ,{useState} from 'react';
import {
View,
Text,
TouchableOpacity,
TextInput,
Platform,
StyleSheet ,
StatusBar,
Alert
} from 'react-native';
import * as Animatable from 'react-native-animatable';
import LinearGradient from 'react-native-linear-gradient';
import FontAwesome from 'react-native-vector-icons/FontAwesome';
import Feather from 'react-native-vector-icons/Feather';
import { useTheme } from 'react-native-paper';
const asdf = ({navigation}) => {
const STORAGE_KEY = 'username';
const [userName, setUserName ] = useState('');
const [errortext, setErrortext] = useState('');
const retrieveData = async () => {
try {
const value = await AsyncStorage.getItem(STORAGE_KEY);
if (value !== null) {
var str = value;
// str = str.replace(/^"|"$/g, '');
str = str.replace(/"/g,'');
fetchData(str);
}
} catch (error) {
// Error retrieving data
}
};
retrieveData();
const handleSubmitPress = () => {
if (!userName){
setUserName('')
// onChangeText();
return
} else{
fetchData(userName);
}
setUserName('')
//onChangeText();
}
const fetchData = (userName) => {
if(userName){
let dataToSend = {search :userName}
let formBody = [];
for (let search in dataToSend) {
let encodeKey = encodeURIComponent(search);
let encodeValue = encodeURIComponent(dataToSend[search]);
formBody.push(encodeKey + '=' + encodeValue);
}
formBody = formBody.join('&');
fetch('https://qwert/work/rest/apiofficeapp.php', {
method: 'POST', //Request Type
body: formBody, //post body
headers: {
//Header Defination
'Content-Type':
'application/x-www-form-urlencoded;charset=UTF-8',
},
})
.then((response) => response.json())
.then((responseJson) => {
if (responseJson[0] != ''){
//alert(JSON.stringify(responseJson[0]));
const saveData = async () => {
try {
await AsyncStorage.setItem(STORAGE_KEY, responseJson[0][0]['username'])
//await AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(responseJson[0][0]['username']))
//alert('Data successfully saved login page')
} catch (e) {
//alert('Failed to save the data to the storage')
}
}
saveData();
navigation.navigate('HomeApp');
setErrortext('');
}else {
setErrortext(userName + 'user not found')
}
})
.catch((error) => {
console.error(error);
});
} }
// const onChangeText = userName => setUserName(userName)
const onChangeText = userName => setUserName(userName)
const { colors } = useTheme();
// const onChangeText = (val) => {
// if( val.length !== 0 ) {
// setUserName({
// ...userName,
// userName: val,
// });
// } else {
// setUserName({
// ...userName,
// userName: val,
// });
// }
// }
return (
<View style={styles.container}>
<StatusBar backgroundColor='#009387' barStyle="light-content"/>
<View style={styles.header}>
<Text style={styles.text_header}>Welcome!</Text>
</View>
<Animatable.View
animation="fadeInUpBig"
style={[styles.footer, {
backgroundColor: colors.background
}]}
>
<Text style={[styles.text_footer, {
color: colors.text
}]}>Username</Text>
<View style={styles.action}>
<FontAwesome
name="user-o"
color={colors.text}
size={20}
/>
<TextInput
placeholder="Your Username"
placeholderTextColor="#666666"
style={[styles.textInput, {
color: colors.text
}]}
autoCapitalize="none"
onChangeText={(userName) => onChangeText(userName)}
onSubmitEditing={handleSubmitPress}
/>
</View>
{/* {data.check_textInputChange ?
<Animatable.View
animation="bounceIn"
>
<Feather
name="check-circle"
color="red"
size={20}
/>
</Animatable.View>
: null}
</View> */}
<View>
{errortext != '' ? (
<Text style={styles.errorMsg}>
{errortext}
</Text>
) : null}
</View>
<View style={styles.button}>
<TouchableOpacity
style={styles.signIn}
onPress={() => {handleSubmitPress()}}
>
<LinearGradient
colors={['#eb0c00', '#A80900']}
style={styles.signIn}
>
<Text style={[styles.textSign, {
color:'#fff'
}]}> Sign In </Text>
</LinearGradient>
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigation.navigate('Register')}
style={[styles.signIn, {
borderColor: '#A80900',
borderWidth: 1,
marginTop: 15
}]}
>
<Text style={[styles.textSign, {
color: '#A80900'
}]}> Register </Text>
</TouchableOpacity>
</View>
</Animatable.View>
</View>
);
};
export default asdf;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#A80900'
},
header: {
flex: 1,
justifyContent: 'flex-end',
paddingHorizontal: 20,
paddingBottom: 50
},
footer: {
flex: 3,
backgroundColor: '#fff',
borderTopLeftRadius: 30,
borderTopRightRadius: 30,
paddingHorizontal: 20,
paddingVertical: 30
},
text_header: {
color: '#fff',
fontWeight: 'bold',
fontSize: 30
},
text_footer: {
color: '#05375a',
fontSize: 18
},
action: {
flexDirection: 'row',
marginTop: 10,
borderBottomWidth: 1,
borderBottomColor: '#f2f2f2',
paddingBottom: 5
},
actionError: {
flexDirection: 'row',
marginTop: 10,
borderBottomWidth: 1,
borderBottomColor: '#FF0000',
paddingBottom: 5
},
textInput: {
flex: 1,
marginTop: Platform.OS === 'ios' ? 0 : -12,
paddingLeft: 10,
color: '#05375a',
},
errorMsg: {
color: '#FF0000',
fontSize: 14,
},
button: {
alignItems: 'center',
marginTop: 50
},
signIn: {
width: '100%',
height: 50,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 10
},
textSign: {
fontSize: 18,
fontWeight: 'bold'
}
});
I not able to navigate to home app without showing the login screen directly
One thing you can try is to use a Splash lib that will create an actual native screen for the splash and you just dismiss the splash screen on the login screen or the main screen.
One lib you may use:
https://github.com/crazycodeboy/react-native-splash-screen
It fairly simple to install and you just need to call this on your Login or Mais screen when its mounted:
import SplashScreen from 'react-native-splash-screen'
export default class MainPage extends Component {
componentDidMount() {
SplashScreen.hide();
}
}
Success on your project.

Expo location not have address module

I'm trying to get current location for my project but it's giving me this error even I installed expo-location
Can someone help me fix this error?
Thank you so much
Remove <Location.Address>
Final Output:
Here is the full working example.
Use your own Maps API Key to use reverseGeocodeAsync.
import React, { useEffect, useState } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
// You can import from local files
import AssetExample from './components/AssetExample';
let apiKey = 'YOUR_API_KEYS';
// or any pure javascript modules available in npm
import { Card } from 'react-native-paper';
import * as Location from 'expo-location';
export default function App() {
const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
const [address, setAddress] = useState(null);
const [getLocation, setGetLocation] = useState(false);
useEffect(() => {
(async () => {
let { status } = await Location.requestPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
}
Location.setGoogleApiKey(apiKey);
console.log(status);
let { coords } = await Location.getCurrentPositionAsync();
setLocation(coords);
console.log(coords);
if (coords) {
let { longitude, latitude } = coords;
let regionName = await Location.reverseGeocodeAsync({
longitude,
latitude,
});
setAddress(regionName[0]);
console.log(regionName, 'nothing');
}
// console.log();
})();
}, [getLocation]);
return (
<View style={styles.container}>
<Text style={styles.big}>
{!location
? 'Waiting'
: `Lat: ${location.latitude} \nLong: ${
location.longitude
} \n${JSON.stringify(address?.["subregion"])}`}
</Text>
<TouchableOpacity onPress={() => setGetLocation(!getLocation)}>
<View
style={{
height: 100,
backgroundColor: 'teal',
justifyContent: 'center',
alignItems: 'center',
borderRadius: 10,
marginTop: 20,
}}>
<Text style={styles.btnText}> GET LOCATION </Text>
</View>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
alignItems: 'center',
justifyContent: 'center',
},
big: {
fontSize: 18,
color: 'black',
fontWeight: 'bold',
},
btnText: {
fontWeight: 'bold',
fontSize: 25,
color: 'white',
},
});
You can play with the working code here: Expo Link

Im new to react and I am having a ton of issues trying to get data fields from firestore to show up on the screen on my app

I am going to include photos as well as my code. I do not know what I am doing wrong but I wanna be able to print the current user's name on their profile page as shown in pictures. Thanks so much! It seems as if it is never getting to the snapshot to try to retrieve the fields to where they can be used. I've been stuck on this for going on a week now and do not know what else to try. I feel like it is an easy fix that I am missing but cannot pinpoint what it is.
Firestore picture
App picture
import 'react-native-gesture-handler';
import auth, {firebase} from '#react-native-firebase/auth';
import firestore from '#react-native-firebase/firestore';
//import * as React from 'react';
import React, {useState} from 'react';
import {NavigationContainer} from '#react-navigation/native';
import {
StyleSheet,
Picker,
SafeAreaView,
Text,
View,
TextInput,
TouchableOpacity,
Button,
} from 'react-native';
import {createStackNavigator} from '#react-navigation/stack';
import {ScrollView} from 'react-native-gesture-handler';
const Profile = ({navigation}) => {
const [firstName, setFname] = useState('');
const [lastName, setLname] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [age, setAge] = useState('');
const [sex, setSex] = useState('');
const [id, setId] = useState('');
getUserInfo = async () => {
firebase
.firestore()
.collection('users')
.doc(firebase.auth().currentUser.uid)
.onSnapshot((docs) => {
setFname(docs.data().firstName);
console.log(firstName);
setLname({lastName: docs.data().lastName});
setEmail({email: docs.data().email});
setPassword({password: docs.data().password});
setAge({age: docs.data().age});
setSex({sex: docs.data().sex});
setId({id: docs.data().id});
});
};
const db = firebase.firestore();
var user = firebase.auth().currentUser;
var usersEmail = user.email;
var userID = firebase.auth().currentUser.uid;
db.collection('users')
.where('email', '==', usersEmail)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, ' => ', doc.data());
var data = doc.data();
const fn = doc.get('firstName');
setFname({firstName: fn});
console.log(firstName);
console.log(data);
setFname(data.firstName);
//var lastName = data.lastName;
//var firstName = doc.get("first");
//var lastName = doc.get("last");
});
})
.catch(function (error) {
console.log('Error getting documents: ', error);
});
return (
<SafeAreaView style={styles.container2}>
<ScrollView>
<View style={styles.container}>
<Text style={styles.logo}>Profile</Text>
<Text style={styles.Info}>
Name: {firstName} {lastName}
</Text>
<Text style={styles.Info}>Email: {usersEmail}</Text>
<Text style={styles.Info}>Age: </Text>
<Text style={styles.Info}>Sex:</Text>
<Text style={styles.Info}>Sports:</Text>
<TouchableOpacity
style={styles.button}
onPress={() => navigation.navigate('EditProfile')}>
<Text style={styles.buttonText}>Edit Profile</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => navigation.navigate('Messages')}>
<Text style={styles.buttonText}>My Messages</Text>
</TouchableOpacity>
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#2c9f45',
alignItems: 'center',
justifyContent: 'center',
},
container2: {
flex: 1,
backgroundColor: '#2c9f45',
justifyContent: 'center',
},
button: {
width: '85%',
backgroundColor: '#263844',
borderRadius: 25,
height: 54,
alignItems: 'center',
justifyContent: 'center',
marginTop: 15,
marginBottom: 15,
},
buttonText: {
color: 'white',
fontSize: 20,
},
logo: {
fontWeight: 'bold',
fontSize: 54,
color: '#263844',
marginBottom: 20,
},
Info: {
fontWeight: 'bold',
fontSize: 22,
color: '#263844',
marginBottom: 20,
},
});
export default Profile;
Its all getting render before the results have been returned, you need include useEffect and run your firebase from within inside the useEffect function.
This is pretty decent blog and will help with the understanding of hooks.
Something like this (untested);
import 'react-native-gesture-handler';
import auth, {firebase} from '#react-native-firebase/auth';
import firestore from '#react-native-firebase/firestore';
//import * as React from 'react';
import React, {useEffect, useState} from 'react';
import {NavigationContainer} from '#react-navigation/native';
import {
StyleSheet,
Picker,
SafeAreaView,
Text,
View,
TextInput,
TouchableOpacity,
Button,
} from 'react-native';
import {createStackNavigator} from '#react-navigation/stack';
import {ScrollView} from 'react-native-gesture-handler';
const Profile = ({navigation}) => {
const [firstName, setFname] = useState('');
// const db = firebase.firestore();
// const { email } = firebase.auth().currentUser;
const email = ‘Sprint3#test.com’;
useEffect( () => {
db.collection('users')
.where('email', '==', usersEmail)
.get()
.then(function (querySnapshot) {
// This will step over every doc that has that email, you should limit it one and organise by an updated field. .limit(1)
querySnapshot.forEach(function (doc) {
const fn = doc.get('firstName');
setFname(fn);
// Add whatever states from the data you need or just one object with all the info in.
});
})
.catch(function (error) {
console.log('Error getting documents: ', error);
});
}, []);
return (
<SafeAreaView style={styles.container2}>
<ScrollView>
<View style={styles.container}>
<Text style={styles.logo}>Profile</Text>
<Text style={styles.Info}>
Name: {firstName}
</Text>
<TouchableOpacity
style={styles.button}
onPress={() => navigation.navigate('EditProfile')}>
<Text style={styles.buttonText}>Edit Profile</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.button}
onPress={() => navigation.navigate('Messages')}>
<Text style={styles.buttonText}>My Messages</Text>
</TouchableOpacity>
</View>
</ScrollView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#2c9f45',
alignItems: 'center',
justifyContent: 'center',
},
container2: {
flex: 1,
backgroundColor: '#2c9f45',
justifyContent: 'center',
},
button: {
width: '85%',
backgroundColor: '#263844',
borderRadius: 25,
height: 54,
alignItems: 'center',
justifyContent: 'center',
marginTop: 15,
marginBottom: 15,
},
buttonText: {
color: 'white',
fontSize: 20,
},
logo: {
fontWeight: 'bold',
fontSize: 54,
color: '#263844',
marginBottom: 20,
},
Info: {
fontWeight: 'bold',
fontSize: 22,
color: '#263844',
marginBottom: 20,
},
});
export default Profile;

Resources