AsyncStorage not storing data and subsequently does not retrieve data - reactjs

I am using expo and react-native
I am trying to use AsyncStorage to save 3 values in persistent storage
so I can get it from anywhere in the app,
but asyncstorage is not saving or retrieving,
I have rewritten the code several times and several diffrent ways and have tried a few tutorials, nothing works,
so here is the code for the page where i save the data (Login.js)
import React, { useState} from 'react'
import { View, Text,TextInput ,Button,SafeAreaView,Image,} from 'react-native'
import { ProgressDialog,Dialog } from 'react-native-simple-dialogs';
import AsyncStorage from '#react-native-community/async-storage';
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
function Login ({navigation}){
const[alerted,isalerted]=useState(false)
const[authError,setAuthError]=useState(false)
const[accInactive,isAccInactive]=useState(false)
const[loginFail,didLoginFail]=useState(false)
const[login,didLogin]=useState(false)
const[email,setemail]=useState(email)
const[ClientID,setClientID]=useState(ClientID)
const[Password,setPassword]=useState(Password)
Here I am Trying to set the items for Asyncstorage
function LoginButton () {
AsyncStorage.setItem("MyEmail",email)
AsyncStorage.setItem("MyPassword",Password)
AsyncStorage.setItem("MyClientID",ClientID)
fetch('https://www.SITE.co.za/app-login.asp?ClientID='+ClientID+'&Username='+email+'&Pwd='+Password)
.then((response) => response.json())
.then((responseJson) => {
//Successful response from the API Call
if (JSON.stringify(responseJson).includes("Login Success")){
{isalerted(true),didLogin(false),isLoggedIn('Yes')}
}
else if (JSON.stringify(responseJson).includes("No Authentication Details Received"))
{
{setAuthError(true)}
}
else if (JSON.stringify(responseJson).includes("Account Not Active"))
{
{isAccInactive(true)}
}
else if (JSON.stringify(responseJson).includes("Login Details Incorrect"))
{
{didLoginFail(true)}
}
})
.catch((error) => {
console.error(error);
});
}
function navButton(){
navigation.navigate('Dashboard')
isalerted(false)
}
return (
<KeyboardAwareScrollView keyboardShouldPersistTaps='handled'>
<SafeAreaView>
<ProgressDialog
visible={didLogin}
activityIndicatorColor="#4994CF"
activityIndicatorSize="large"
animationType="slide"
title="Logging in"
message="Please, wait..."
/>
<Dialog
visible={alerted}
onTouchOutside={''} >
<View>
<Text style={{color:'green',fontSize:20, marginBottom:10 ,textAlign:"center"}}>Login Success</Text>
<Button
title="Continue To Dashboard"
onPress={navButton}
/>
</View>
</Dialog>
<Dialog
visible={authError}
onTouchOutside={''} >
<View>
<Text style={{color:'red',fontSize:20, marginBottom:10 ,textAlign:"center"}}>No Authentication Details Received</Text>
<Button
title="OK"
onPress={ ()=>setAuthError(false)}
/>
</View>
</Dialog>
<Dialog
visible={accInactive}
onTouchOutside={''} >
<View>
<Text style={{color:'red',fontSize:20, marginBottom:10 ,textAlign:"center"}}>Account Not Active</Text>
<Button
title="OK"
onPress={ () =>isAccInactive(false)}
/>
</View>
</Dialog>
<Dialog
visible={loginFail}
onTouchOutside={''} >
<View>
<Text style={{color:'red',fontSize:20, marginBottom:10 ,textAlign:"center"}}>Login Details Incorrect</Text>
<Button
title="OK"
onPress={()=> didLoginFail(false)}
/>
</View>
</Dialog>
<View style={{flexDirection:"row"}}>
<Image source={{uri: 'https://www.SITEe.co.za/images/smartpractice-logo-02.png'}}
style={{marginTop:35,paddingTop:10
, height: 80,width:360,flexWrap:'wrap',resizeMode:"contain" }} />
<Text style={{textAlign:'center',color:"#4994CF", marginTop:35}}>Beta</Text>
</View>
<View style={{width:'95%',alignContent:'center',marginLeft:10}}>
<Text style={{fontSize:20,color:'grey'}}>Welcome to the SmartPractice Beta App,
Please Login below with your SmartPractice login Details
</Text>
</View>
<View style={{marginTop:10,alignItems:"center"}}>
<View style={styles.Label}>
<Text style={styles.LabelText}>Email</Text>
</View>
<TextInput onChangeText={(text)=>setemail(text)} autoCompleteType='email' placeholder="Email" style={{width:'95%',height:40,backgroundColor:'#d8d8d8',marginBottom:3, paddingHorizontal: 10,marginTop:5}}/>
<View style={styles.Label}>
<Text style={styles.LabelText}>Password</Text>
</View>
<TextInput onChangeText={(text)=>setPassword(text)} autoCompleteType="password" secureTextEntry={true} placeholder="Password" style={{width:'95%',height:40,backgroundColor:'#d8d8d8',marginBottom:3, paddingHorizontal: 10,marginTop:5}}/>
<View style={styles.Label}>
<Text style={styles.LabelText}>ClientID</Text>
</View>
<TextInput onChangeText={(text)=>setClientID(text)} keyboardType="numeric" placeholder="ClientID" style={{width:'95%',height:40,backgroundColor:'#d8d8d8',marginBottom:3, paddingHorizontal: 10,marginTop:5}}/>
<View style={{marginTop:5,width:'95%'}}>
<View style={{width:'95%',alignContent:'center',marginLeft:10,marginBottom:10}}>
<Text style={{fontSize:15,color:'grey'}}>ClientID : Log into Smartpractice on the internet and use the 4 digit number at the end of your URL (internet Address) of your login screen
</Text>
</View>
<Button
title="Save"
onPress={LoginButton}
/>
</View>
</View>
</SafeAreaView>
</KeyboardAwareScrollView>
);
}
const styles = {
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
marginTop:5
},
Label: {
backgroundColor:'#A6A5A4',
color:"white",
textAlign: "center",
marginTop:2,
fontSize: 15,
width:'100%',
},
alertmessage:{
color:'green'
},
LabelText: {
backgroundColor:'#A6A5A4',
color:"white",
textAlign: "center",
fontSize: 20,
},
};
export default Login;
The here is where I am trying to retrieve some of the data (Dashboard.js)
import React, {Component } from 'react'
import { View, Text,Image,TouchableOpacity,StyleSheet,SafeAreaView,} from 'react-native'
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
import AsyncStorage from '#react-native-community/async-storage';
function Dashboard ({navigation}){
let MyClientID = AsyncStorage.getItem("MyClientID")
console.log(global.mail)
return (
<KeyboardAwareScrollView keyboardShouldPersistTaps='handled'>
<SafeAreaView style={styles.container}>
<View style={{flexDirection:"row"}}>
<Image source={{uri: 'https://www.site.co.za/images/smartpractice-logo-02.png'}}
style={{marginTop:5,paddingTop:10
, height: 80,width:360,flexWrap:'wrap',resizeMode:"contain" }} />
<Text style={{textAlign:'center',color:"#4994CF", marginTop:35}}>Beta</Text>
</View>
<Image source={{uri: 'https://www.site.co.za/images/logos/'+MyClientID+'/main-dashboard-logo.png'}}
style={{marginTop:5,paddingTop:10
, height: 70,width:'100%',flexWrap:'wrap',resizeMode:"contain",marginLeft:5 }} />
<View style={{justifyContent:"center", marginTop:6}}>
<Text style={styles.DateLabel}>Description</Text>
</View>
<TouchableOpacity style={styles.ImageButton}
onPress={() => navigation.navigate('Incidental Time')}
>
<Image style={{flexWrap:'wrap',resizeMode:"contain",maxHeight:120,maxWidth:120 }} tintColor='#4994CF' source={require('../../icons/time_log.png')} />
<Text style={styles.TextStyle}>Time Log</Text>
</TouchableOpacity>
</SafeAreaView>
</KeyboardAwareScrollView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
textInput: {
backgroundColor:'#A6A5A4',
color:"white",
textAlign: "center",
marginTop:5,
fontSize: 20,
marginBottom:5
,marginLeft:1
},
DateLabel: {
backgroundColor:'#A6A5A4',
color:"white",
textAlign: "center",
marginTop:2,
fontSize: 20,
paddingLeft:140,
paddingRight:140,
width:'100%',
marginBottom:10
},
ImageButton: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
TextStyle:{
color:"grey"
}
});
export default Dashboard;
I have tried setting the items in a useeffect wrapping the values in the setitems in STRING()
tried making functions and const with async attached and the await before the set/getitem
I have check SO for hours and none of the answers or questions have helped me with this
I seriously dont know what I am doing wrong
I followed the asynstorage docs and even copying the code from the docs does not work
I dont get errors, Its been 2 days of struggling with something as simple as local storage
Please help

Both getItem and setItem of AsyncStorage are Promise so you need to use await or callback.
For setItem, current usage is okay but for getItem, it is essential.
You also need to use getItem in useEffect and make MyClientID as a state.
Try the following code instead of your Dashborad.js:
import React, {Component} from 'react';
import {
View,
Text,
Image,
TouchableOpacity,
StyleSheet,
SafeAreaView,
} from 'react-native';
import {KeyboardAwareScrollView} from 'react-native-keyboard-aware-scroll-view';
import AsyncStorage from '#react-native-community/async-storage';
function Dashboard({navigation}) {
const [MyClientID, setMyClientID] = useState('');
useEffect(() => {
AsyncStorage.getItem('MyClientID').then((myClientID) => {
setMyClientID(myClientID);
});
}, []);
console.log(global.mail);
return (
<KeyboardAwareScrollView keyboardShouldPersistTaps="handled">
<SafeAreaView style={styles.container}>
<View style={{flexDirection: 'row'}}>
<Image
source={{
uri: 'https://www.site.co.za/images/smartpractice-logo-02.png',
}}
style={{
marginTop: 5,
paddingTop: 10,
height: 80,
width: 360,
flexWrap: 'wrap',
resizeMode: 'contain',
}}
/>
<Text style={{textAlign: 'center', color: '#4994CF', marginTop: 35}}>
Beta
</Text>
</View>
<Image
source={{
uri:
'https://www.site.co.za/images/logos/' +
MyClientID +
'/main-dashboard-logo.png',
}}
style={{
marginTop: 5,
paddingTop: 10,
height: 70,
width: '100%',
flexWrap: 'wrap',
resizeMode: 'contain',
marginLeft: 5,
}}
/>
<View style={{justifyContent: 'center', marginTop: 6}}>
<Text style={styles.DateLabel}>Description</Text>
</View>
<TouchableOpacity
style={styles.ImageButton}
onPress={() => navigation.navigate('Incidental Time')}>
<Image
style={{
flexWrap: 'wrap',
resizeMode: 'contain',
maxHeight: 120,
maxWidth: 120,
}}
tintColor="#4994CF"
source={require('../../icons/time_log.png')}
/>
<Text style={styles.TextStyle}>Time Log</Text>
</TouchableOpacity>
</SafeAreaView>
</KeyboardAwareScrollView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
textInput: {
backgroundColor: '#A6A5A4',
color: 'white',
textAlign: 'center',
marginTop: 5,
fontSize: 20,
marginBottom: 5,
marginLeft: 1,
},
DateLabel: {
backgroundColor: '#A6A5A4',
color: 'white',
textAlign: 'center',
marginTop: 2,
fontSize: 20,
paddingLeft: 140,
paddingRight: 140,
width: '100%',
marginBottom: 10,
},
ImageButton: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
TextStyle: {
color: 'grey',
},
});
export default Dashboard;

Not related to your Query but to be honest, based from my experience, Expo is the last thing you wanna use in your project. Especially if you'll be introducing Native dependencies later.
I know you can use ExpoKit then, but still... Don't use Expo
Instead of KeyboardAwareScrollView you can use Content from NativeBase.
For your query, all methods in AsyncStorage are Asynchronous. So remember to use await to get your values
async function fetchLocal(){
const persisted = await AsyncStorage.getItem("persisted");
if(persisted){
console.log(JSON.parse(persisted)); // Oh! the value for these persisted keys needs to be a string, so make sure to String()|.toString()|JSON.stringify objects you wanna serialize.
}
}

Related

How to update screen dimensions in react native

I have been trying to update my screen dimensions in react native so that the component resizes on-screen rotation with no vain. I have created my style in a separate file (Styles.js) and imported the styles in my login page like below:
Styles.js
import {
StyleSheet,
Dimensions
} from "react-native";
import {
scale,
ScaledSheet
} from "react-native-size-matters";
const styles = ScaledSheet.create({
scrollViewContainer: {
backgroundColor: '#E20030',
alignItems: 'center',
height: Dimensions.get('window').height,
padding: '50#s'
},
container2: {
alignItems: 'center',
justifyContent: 'center',
width: Dimensions.get('window').width,
flex: 1,
},
loginRect: {
borderRadius: '30#s',
marginTop: '20#s',
width: Dimensions.get('window').width * 0.8,
backgroundColor: 'white',
alignItems: 'center'
},
SectionStyle: {
flexDirection: 'row',
marginTop: 0,
marginLeft: '35#s',
marginRight: '35#s',
borderWidth: '1#s',
borderRadius: '5#s',
borderColor: '#dadae8',
},
});
Login.js
import React, { } from 'react';
import {
Text, View, TextInput, TouchableOpacity,
KeyboardAvoidingView,
} from 'react-native';
import { mystyles, styles } from './Components/styles';
const LoginScreen = () => {
return (
<View style={mystyles.scrollViewContainer}>
<KeyboardAvoidingView behavior="padding" style={{ flex: 100 }}>
<View style={mystyles.scrollViewContainer}>
<View style={mystyles.loginRect}>
<View style={{ flexDirection: 'row' }}>
<View style={{ flex: 1, marginLeft: 45, marginTop: 20 }}>
<Text style={{ color: 'black' }}>Username</Text>
</View>
</View>
<View style={styles.SectionStyle}>
<TextInput
style={styles.input}
/>
</View>
<View style={{ flexDirection: 'row' }}>
<View style={{ flex: 1, marginLeft: 45, marginTop: 20 }}>
<Text style={{ color: 'black' }}>Password</Text>
</View>
</View>
<View style={styles.SectionStyle}>
<TextInput
style={styles.input}
/>
</View>
<TouchableOpacity>
<Text style={styles.buttonTextStyle}>LOGIN</Text>
</TouchableOpacity>
</View>
</View>
</KeyboardAvoidingView>
</View>
)
}
export default LoginScreen;
So I have tried using Dimensions onchange listener in my Styles.js but I get an error
ERROR Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
I created this function in the Styles.js file
m
function getDimensions() {
const [dimensions, setDimensions] = useState({
window,
screen
});
useEffect(() => {
const subscription = Dimensions.addEventListener(
"change",
({
window,
screen
}) => {
setDimensions({
window,
screen
});
console.log(dimensions.window.height + " +++ " + dimensions.screen)
}
);
// return () => subscription?.remove();
});
return dimensions.window;
}
Is there any way I can update the component sizes once the screen has been rotated?
This is working for me to detect rotation or changes in the size of the screen (split mode in tablets for example) without eventListeners.
import { Dimensions } from "react-native";
...
useEffect(() => {
...
}, [Dimensions.get("screen").height, Dimensions.get("screen").width]);
Hope this help to anybody

React Native how to create a delete button that deletes a different element as well as itself

Look at my code below please I am trying to find a way to make a delete button but i am very new to react native, i have been trying to do it for the past few hours.
import React, { useState } from 'react';
import { StyleSheet, FlatList, Text, View, Image, TextInput, Button, Keyboard, TouchableOpacity, CheckBox } from 'react-native';
import Interactable from 'react-native-interactable';
export default function App()
const [enteredGoal, setEnteredGoal] = useState('');
const [courseGoals, setCourseGoals] = useState([]);
const goalInputHandler = (enteredText) => {
setEnteredGoal(enteredText);
};
const addGoalHandler = () => {
if (enteredGoal.length > 0) {
setCourseGoals(currentGoals => [...currentGoals, enteredGoal])
} else {
alert("You have to write something!")
}
}
return (
<View style={styles.container}>
<View style={styles.topPart}></View>
<View style={styles.navBar}>
<Image source={require('./assets/baseline_menu_black_18dp.png/')} />
<Text style={styles.heading}> Grocery List </Text>
</View>
<View style={styles.body}>
<TextInput
style={styles.textInput}
placeholder='Groceries'
maxLength={20}
onBlur={Keyboard.dismiss}
value={enteredGoal}
onChangeText={goalInputHandler}
/>
<View style={styles.inputContainer}>
<TouchableOpacity style={styles.saveButton}>
<Button title="ADD" onPress={addGoalHandler} color="#FFFFFF" style={styles.saveButtonText} />
</TouchableOpacity>
</View>
<View style={styles.container}>
<FlatList
data={courseGoals}
renderItem={itemData => (
<View style={styles.groceryItem} >
<Text style={styles.groceryItemText}>{itemData.item}</Text>
<Text style={styles.groceryItemDelete}>X</Text>
</View>
)}
/>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
topPart: {
height: '3%',
backgroundColor: '#5498A7',
},
navBar: {
height: '10%',
backgroundColor: '#5498A7',
elevation: 3,
paddingHorizontal: 15,
flexDirection: 'row',
alignItems: 'center',
},
body: {
backgroundColor: '#edebe9',
height: '100%',
flex: 1
},
heading: {
fontWeight: "bold",
justifyContent: 'center',
paddingLeft: '13%',
fontSize: 25,
color: '#d6d4d3'
},
textInput: {
borderColor: '#CCCCCC',
borderTopWidth: 1,
borderBottomWidth: 1,
height: 50,
fontSize: 25,
paddingLeft: 20,
paddingRight: 20
},
saveButton: {
borderWidth: 1,
borderColor: '#5498A7',
backgroundColor: '#5498A7',
padding: 15,
margin: 5,
},
saveButtonText: {
color: '#FFFFFF',
fontSize: 20,
textAlign: 'center'
},
groceryItem: {
borderWidth: 1,
borderColor: 'black',
backgroundColor: '#6A686B',
padding: 15,
margin: 5,
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between'
},
groceryItemText: {
color: '#d6d4d3',
},
groceryItemDelete: {
color: 'red',
fontWeight: 'bold',
fontSize: 20
}
});
I hope you guys can find a solution I will keep u guys updated on my progress, I am constantly looking for answers but i don't really understand how to make the delete (X) work and to make it delete a different element, if you know a way to make work better just let me know i would appreciate it a lot
You have to send index to delete function.
renderItem={(itemData,index) => (
<View style={styles.groceryItem} >
<Text style={styles.groceryItemText}>{itemData.item}</Text>
<Text style={styles.groceryItemDelete.bind(index)}>X</Text>
</View>
and example delete function :
groceryItemDelete(index){
setCourseGoals(currentGoals.splice(index,1))
}
You need to implement the delete method and add an onPress event:
renderItem={(itemData, idx) => (
<View style={styles.groceryItem} >
<Text style={styles.groceryItemText}>{itemData.item}</Text>
<Text style={styles.groceryItemDelete} onPress={() => deleteItem(idx)}>X</Text>
</View>
)}
const deleteItem = idx => {
const clonedGoals = [...courseGoals]
clonedGoals.splice(idx, 1)
setCourseGoals(clonedGoals)
}

Getting a navigation.navigate type error after spliting the mainscreen into components

I was working on my app with two screens a Main Screen and a Details Screen everything was working, but the code was long, so I tried to split everything into reusable components which caused a
navigation.navigates type error for some reason.
I checked the code multiple times everything makes perfect sense to me is there something am missing here, how do I fixed this error?
Am using react navigation version 5 for the first time.
Here is a sample of the code used:
MainScreen.js
import React from "react";
import { StyleSheet, Text, View, Image, FlatList } from "react-native";
import ArticleList from "../components/ArticleList";
function MainScreen() {
return (
<View style={{ flex: 1 }}>
{/* show the data in a flatlist */}
<ArticleList />
</View>
);
}
MainScreen.navigationOptions = () => {
return {
headerShown: false,
};
};
export default MainScreen;
DetailScreen.js
import React from "react";
import { StyleSheet, Text, View, Dimensions, Image } from "react-native";
import { Feather } from "#expo/vector-icons";
import { SharedElement } from "react-native-shared-element";
import { TouchableOpacity, ScrollView } from "react-native-gesture-handler";
const DetailScreen = (props) => {
const { width, height } = Dimensions.get("window");
const { data } = props.route.params;
return (
<View style={styles.container}>
<View>
<SharedElement id={`item.${data.id}.photo`}>
<Image
resizeMode="cover"
source={{ uri: data.image }}
style={{
width: 400,
height: 300,
borderBottomLeftRadius: 10,
borderBottomRightRadius: 10,
}}
/>
</SharedElement>
<View
style={{
flexDirection: "row",
alignItems: "center",
position: "absolute",
bottom: 14,
left: 10,
}}
>
<SharedElement id={`item.${data.id}.profilePic`}>
<Image
resizeMode="cover"
source={{ uri: data.profilePic }}
style={{
width: 60,
height: 60,
borderRadius: 10,
marginRight: 14,
}}
/>
</SharedElement>
<View
style={{
flex: 1,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
}}
>
<View>
<SharedElement id={`item.${data.id}.username`}>
<Text
style={{ color: "white", fontSize: 16, fontWeight: "bold" }}
>
{data.username}
</Text>
</SharedElement>
<SharedElement id={`item.${data.id}.readtime`}>
<Text style={{ color: "white", fontSize: 14 }}>
{data.readtime}
</Text>
</SharedElement>
</View>
<TouchableOpacity>
<Feather name="bookmark" size={30} color="white" />
</TouchableOpacity>
</View>
</View>
</View>
<ScrollView style={{ paddingHorizontal: 10, paddingTop: 14 }}>
<SharedElement
id={`item.${data.id}.text`}
style={{ width: width - 30, marginBottom: 14 }}
>
<Text style={{ fontSize: 22, fontWeight: "bold", lineHeight: 32 }}>
{data.title}
</Text>
</SharedElement>
<Text
style={{
fontSize: 14,
lineHeight: 28,
textAlign: "justify",
opacity: 0.5,
}}
>
Paragraph 1
</Text>
<Text
style={{
fontSize: 14,
lineHeight: 28,
textAlign: "justify",
opacity: 0.5,
}}
>
Paragraph 2
</Text>
<View
style={{
marginVertical: 25,
paddingBottom: 20,
flex: 1,
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
}}
>
<TouchableOpacity
style={{ flexDirection: "row", padding: 12, alignItems: "center" }}
>
<Feather name="heart" size={16} color="orange" />
<Text style={{ marginHorizontal: 10 }}>3.4k Likes</Text>
</TouchableOpacity>
</View>
</ScrollView>
<View style={{ position: "absolute", top: 40, left: 10 }}>
<TouchableOpacity onPress={() => props.navigation.goBack()}>
<Feather name="arrow-left" size={24} color="white" />
</TouchableOpacity>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
export default DetailScreen;
ArticleList.js
import React from "react";
import {
View,
Text,
StyleSheet,
Image,
Dimensions,
FlatList,
} from "react-native";
import { SharedElement } from "react-native-shared-element";
import TouchableScale from "react-native-touchable-scale";
import { data } from "../data";
function ArticleList({ navigation }) {
const { width, height } = Dimensions.get("window");
return (
<View>
<FlatList
horizontal
showsHorizontalScrollIndicator={false}
style={{ paddingHorizontal: 30 }}
data={data}
keyExtractor={(item) => item.id}
renderItem={({ item }) => {
return (
<View>
<View>
<TouchableScale
activeScale={0.9}
tension={50}
friction={7}
useNativeDriver
onPress={() =>
navigation.navigate("DetailScreen", { data: item })
}
>
{/* to show the horizental news list*/}
<SharedElement id={`item.${item.id}.photo`}>
<Image
source={{ uri: item.image }}
style={{
width: width - 100,
height: height - 350,
borderRadius: 14,
marginRight: 30,
}}
/>
</SharedElement>
{/* to show the news titles inside the pictures*/}
<SharedElement
id={`item.${item.id}.text`}
style={{
width: width - 100,
position: "absolute",
bottom: 90,
left: 10,
paddingHorizontal: 10,
}}
>
<Text style={styles.blogTitle}>{item.title}</Text>
</SharedElement>
{/* to show the pictre of the author of the news article*/}
<View
style={{
flexDirection: "row",
alignItems: "center",
position: "absolute",
bottom: 20,
left: 20,
}}
>
<SharedElement id={`item.${item.id}.profilePic`}>
<Image
resizeMode="cover"
source={{ uri: item.profilePic }}
style={styles.blogProfilePic}
/>
</SharedElement>
</View>
{/* to show the name of the author and read time of article*/}
<View>
<SharedElement id={`item.${item.id}.username`}>
<Text style={styles.blogUsername}>{item.username}</Text>
</SharedElement>
<SharedElement id={`item.${item.id}.readtime`}>
<Text style={styles.readtime}>{item.readtime}</Text>
</SharedElement>
</View>
</TouchableScale>
</View>
</View>
);
}}
/>
</View>
);
}
const styles = StyleSheet.create({
blogTitle: {
color: "white",
fontSize: 24,
fontWeight: "bold",
lineHeight: 28,
},
blogProfilePic: {
height: 50,
width: 50,
borderRadius: 10,
marginRight: 14,
},
blogUsername: {
color: "white",
fontSize: 16,
fontWeight: "bold",
},
readtime: {
fontSize: 14,
color: "white",
},
});
export default ArticleList;
App.js
import React from "react";
import "react-native-gesture-handler";
import { createSharedElementStackNavigator } from "react-navigation-shared-element";
import { NavigationContainer } from "#react-navigation/native";
import MainScreen from "./app/screens/MainScreen";
import DetailScreen from "./app/screens/DetailScreen";
const Stack = createSharedElementStackNavigator();
const App = ({ navigation }) => {
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="MainScreen"
screenOptions={{ headerShown: false }}
>
<Stack.Screen name="MainScreen" component={MainScreen} />
<Stack.Screen
name="DetailScreen"
component={DetailScreen}
options={(navigation) => ({
headerBackTitleVisible: false,
cardStyleInterpolator: ({ current: { progress } }) => {
return {
cardStyle: {
opacity: progress,
},
};
},
})}
sharedElements={(route) => {
const { data } = route.params;
return [
{
id: `item.${data.id}.photo`,
animation: "move",
resize: "clip",
align: "center-top",
},
{
id: `item.${data.id}.text`,
animation: "fade",
resize: "clip",
align: "left-center",
},
{
id: `item.${data.id}.profilePic`,
animation: "move",
resize: "clip",
align: "left-center",
},
{
id: `item.${data.id}.username`,
animation: "fade",
resize: "clip",
align: "left-center",
},
{
id: `item.${data.id}.readtime`,
animation: "fade",
resize: "clip",
align: "left-center",
},
];
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
My apologies for the long samples of code I tried to keep everything that is connected directly to the problem.
The problem is the you are accessing navigation outside the navigation stack. When you moved the flatlist to to ArticleList its outside the navigation and it wont get the navigation prop.
You can handle this in two ways.
You can simply pass the navigation from the main screen
function MainScreen({ navigation }) {
return (
{/* show the data in a flatlist */}
< ArticleList navigation={navigation} />
);
}
You can use the useNavigation hook to access navigation outside navigation.

React native: change button size in center screen

I am new to React Native currently learning about the technology.
I start create a card layout with buttons inside it.
This is my layout :
<KeyboardAvoidingView
behavior='padding'
keyboardVerticalOffset={50}
style={styles.screen}>
<LinearGradient
colors={['#ffedff', '#ffe3ff']}
style={styles.gradient}>
<Card style={styles.card}>
<ScrollView>
<View style={styles.buttonContainer}>
<Button
color={Colors.primary}
title='ورود'
onPress={() => { }} />
</View>
</ScrollView>
</Card>
</LinearGradient>
</KeyboardAvoidingView>
and this is my stylesheet :
const styles = StyleSheet.create({
screen: {
flex: 1
},
gradient: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
card: {
width: '90%',
height: '80%',
padding: 20
},
buttonContainer: {
alignItems: 'center',
marginVertical: 30,
},
});
and result is:
I want to make button bigger so i added width to buttonContainer but button changed position to left of screen and its size does not change.
buttonContainer: {
width: '40%',
alignItems: 'center',
marginVertical: 30,
},
How could i change button width when it is in the center of screen?
So basically alignSelf:'center' was the problem. Removing that worked. Check the expo link below and the code :
import React from 'react';
import {
StyleSheet,
Text,
View,
TextInput,
KeyboardAvoidingView,
ScrollView,
Button
} from 'react-native';
import { LinearGradient } from 'expo-linear-gradient'
import Card from './Card';
export default AuthScreen = () => {
return (
<KeyboardAvoidingView
behavior='padding'
keyboardVerticalOffset={50}
style={styles.screen}>
<LinearGradient
colors={['#ffedff', '#ffe3ff']}
style={styles.gradient}>
<Card style={styles.card}>
<ScrollView>
{/* <View style={styles.wcContainer}>
<Text style={styles.headerText}>خوش آمدید</Text>
</View> */}
{/* <View style={styles.container}>
<Text style={styles.text}>نام کاربری</Text>
<TextInput style={styles.input} />
<Text style={styles.text}>رمز عبور</Text>
<TextInput style={styles.input} />
</View> */}
<View style={styles.buttonContainer}>
<Button
color= 'red'
title='ورود'
style={{marginHorizontal:200}}
onPress={() => { }} />
</View>
</ScrollView>
</Card>
</LinearGradient>
</KeyboardAvoidingView>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1
},
gradient: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
card: {
width: '90%',
height: '80%',
padding: 20
},
buttonContainer: {
// marginHorizontal:200,
width:'100%',
// alignItems: 'center',
// marginVertical: 30,
justifyContent:'center'
},
});
expo link : expo link
hope it helps. feel free for doubts
I fixed this problem with :
buttonContainer: {
width: '40%',
alignSelf: 'center',
marginVertical: 30,
}
Now this is my layout:

Dynamically set values to variables in React/React Native .map function

I want to dynamically set the value to variables in .map function but it appears to syntax errors. Please help me out.
I used to use {} to wrap the JavaScript code in html but this time it didn't work as expected.
Additionally I also wondering that is it possible to access variables stored in the local state object within .map function.
import React from 'react';
import { View, Text } from 'react-native';
import { Icon, Avatar } from 'react-native-elements';
class SearchListItem extends React.Component {
render() {
const { id, title, shortDescription, time, locationDescription, peopleGroup } = this.props.item;
const indexX = -28;
const indexZ = 1.1;
return (
<View style={styles.containerStyle}>
<View style={{ flexDirection: 'row' }}>
<View style={{ width: '90%' }}>
<Text style={styles.title}>{title}</Text>
</View>
<View style={{ width: '10%' }}>
<Icon
name='star-outline'
type='material-community'
color='#666666' />
</View>
</View>
<View>
<Text style={{ marginTop: 5, color: '#666666' }}>{shortDescription}</Text>
<Text style={{ marginTop: 5 }}>{time}</Text>
<Text style={{ marginTop: 5 }}>{locationDescription}</Text>
</View>
<View style={{ flexDirection: 'row', marginTop: 20 }}>
{const i = 0}
peopleGroup.map((people) => (
<Avatar
width={30}
position='absolute'
containerStyle={{ transform: [{translate: [-28 + (28 * i), 0, 20]}] }}
small
rounded
source={{uri: "http://www.5seestar.com/jiaoxuewen/images/1701/dengchao.jpg"}}
activeOpacity={0.7}
/> ));
</View>
</View>
);
}
};
const styles = {
containerStyle: {
padding: 30,
flexDirection: 'column',
alignItems: 'flex-start',
justifyContent: 'center',
marginTop: 5
},
title: {
fontWeight: 'bold',
fontSize: 18
}
};
export default SearchListItem;
All you want to do is to use the index of the mapped value, so you can use this i guess :
<View style={{ flexDirection: 'row', marginTop: 20 }}>
{
peopleGroup.map((people,i) => (
<Avatar
width={30}
position='absolute'
containerStyle={{ transform: [{translate: [-28 + (28 * i), 0, 20]}] }}
small
rounded
source={{uri:"http://www.5seestar.com/jiaoxuewen/images/1701/dengchao.jpg"}}
activeOpacity={0.7}
/>
));
}
</View>

Resources