Navigation With Flatlist Data in React Native - react-native

Hello Here i have some data as the form of Flatlist and i have extracted it, now i want to move on that
screen that i have clicked on example
if i clicked on screen A then i will moved to screen A,
if i clicked on screen B then i will moved on screen B,
if i clicked on screen C then i will moved to screen C,
if i clicked on screen D then i will moved on screen D, And
Show their Header Also
How to call proper screen according to their header and screen..
How to Navigate it..
Sample image here
Code Here..
import React, {useState} from 'react';
import {
FlatList,
Image,
View,
Text,
SafeAreaView,
StyleSheet,
TouchableOpacity
} from 'react-native';
import App1 from './App1';
const dummyArray = [
{id: '1', value: 'A',exdate: '2020', City: 'Delhi'},
{id: '2', value: 'A',exdate: '2019',City: 'Mumbai'},
{id: '3', value: 'C',exdate: '2015 ',City: 'Indore'},
{id: '4', value: 'D',exdate: '2016',City: 'Patna'},
{id: '5', value: 'E',exdate: '2000',City: 'Raipur'},
];
const Home = ({ navigation }) => {
const [listItems, setListItems] = useState(dummyArray);
function handlePick(item){
}
const ItemView = ({item}) => { //flatlist data view
return (
// FlatList Item
<View style={styles.itemView}>
<TouchableOpacity style={styles.button} activeOpacity={.5}
onPress={()=>handlePick(item)}>
<View style={styles.stateView}>
<Text style={styles.textItem} onPress={() => getItem(item)}>
{item.value}
</Text>
<Image source={require('./right.jpg')} style={{marginLeft: 70, marginTop: 5,width: 30, height: 30}} />
</View>
<View
style={{
marginTop: 3,
height: 1,
width: '100%',
backgroundColor: '#C8C8C8'
}}
/>
<Text style={styles.text}>Date{"\t\t\t\t"}{item.exdate}</Text> //flatlist Data
<Text style={styles.capitalText}>Capital City{"\t\t\t\t\t\t"}{item.City}</Text> //flatlistCity
</TouchableOpacity>
</View>
);
};
const ItemSeparatorView = () => {
return (
// FlatList Item Separator
<View
style={{
backgroundColor: '#C8C8C8'
}}
/>
);
};
};
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<FlatList
data={listItems}
ItemSeparatorComponent={ItemSeparatorView}
renderItem={ItemView}
keyExtractor={(item, index) => index.toString()}
/>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
flex: 1,
marginLeft: 10,
marginRight: 10,
marginBottom: 10,
},
textItem: {
marginTop: 5,
fontSize: 18,
flexDirection: 'row',
fontWeight: 'bold',
height: 20,
width: 250,
},
itemView: {
height: 150,
padding: 10,
margin: 8,
backgroundColor: '#fff'
},
stateView:{
height: 40,
flexDirection: 'row',
},
text:{
marginTop: 5,
width: 300,
height: 28
},
});
export default Home;
Please Suggest any solution..
Thanks..

Try this way
function handlePick(item){
const route = `Screen${item.value}`; // dynamic screen like 'ScreenA' or 'ScreenB' etc...
navigation.navigate(route, {data: item} ); This way will navigate to route with data object of item
}

Related

React Native Alert doesn't accept Navigation in my code

I'm trying to add a navigation in this Alert with Dialog, in the 'OK' option to confirm Logout and go back to the login screen, however it's not working. Does anyone help me with this?
I'm also trying to insert a dark theme for the entire application, local expo finger printing and push notifications in setData options 2, 3, and 4, but it's a difficult task as well.. But I'm more focused on creating this Navigation in my Alert because I'm trying.
i'll be very grateful for the help :)
when I grow up, I'm going to be a great programmer.
import { SafeAreaView,Text, View, FlatList,
TouchableOpacity, StyleSheet, Image, Switch, Alert} from 'react-native';
import { Entypo } from 'react-native-vector-icons';
import React, {useState, useEffect} from 'react';
import { useNavigation } from '#react-navigation/native';
import * as LocalAuthentication from 'expo-local-authentication';
//import Auth from './Biometric'
const Settings = () =>{
const [data, setData] = useState([
{ id: 1, text: 'Perfil', image: require('../../assets/images/user.png'), chosen: false },
{ id: 2, text: 'TouchId', image: require('../../assets/images/fingerprint.png'), chosen: false },
{ id: 3, text: 'Dark/Light mode', image: require('../../assets/images/light-up.png'), chosen: false },
{ id: 4, text: 'Notificações', image: require('../../assets/images/bell-fill.png'), chosen: false },
]);
const [isRender, setisRender] = useState(false);
const navigation = useNavigation();
const handleLogout = () => {
//navigation.navigate();
Alert.alert('Logout!', 'Deseja realmente sair?', [
{
text: 'Cancelar',
onPress: () => {},
},
{
text: 'OK',
onPress:()=>
{navigation.navigate ("Login")}
},
]);
}
const renderItem = ({ item }) => {
return (
<TouchableOpacity
style={styles.item}
>
<View style={styles.avatarContainer}>
<Image source={item.image} style={styles.avatar} />
</View>
<View>
<Text style={styles.text}>{item.text}</Text>
</View>
{item.id > 1 && <Switch style={{ width: 10, alignItems: 'flex-end',
marginTop: 15, flex: 1, marginEnd: 30}}
thumbColor={item.chosen == false ? "#CDCDCD" : "#A0D800"}
trackColor={{ true: "#CDCDCD" }}
value={item.chosen}
onChange={() => setData(data.map(index => item.id === index.id
? { ...index, chosen: !index.chosen }
: index
))} />}
{item.id === 2
}
</TouchableOpacity>
);
};
return (
<SafeAreaView style={styles.container}>
<FlatList
data={data}
keyExtractor={(item) => item.id.toString()}
renderItem={renderItem}
extraData={isRender} />
<View style = {{alignSelf: 'center',}}>
<TouchableOpacity
onPress={()=> (handleLogout)}
style = {{borderRadius: 10,
alignItems: "center",
backgroundColor: "#A0D800",height: 50, width: 200,
bottom: 15,
shadowColor: 'rgba(0,0,0, .4)', // IOS
shadowOffset: { height: 1, width: 1 }, // IOS
shadowOpacity: 1, // IOS
shadowRadius: 1, //IOS
elevation: 2, // Android
}}>
<Text style={{color: "#ffffff",
fontWeight: 'bold', fontSize: 17,
padding: 5, bottom: 0, marginTop: 5,
}}>
SignOut
</Text>
<Entypo name={'log-out'}
style={{alignSelf: 'flex-end', bottom: 25,
marginEnd: 25}}
color={'#ffffff'}
size={20}
/>
</TouchableOpacity>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 20
//marginHorizontal: 21
},
item:{
borderBottomWidth: 1,
borderBottomColor: '#808080',
alignItems: 'flex-start',
flexDirection: 'row',
},
avatarContainer: {
backgroundColor: 'transparent',
//borderRadius: 100,
height: 30,
width: 30,
justifyContent: 'center',
alignItems: 'center',
},
avatar: {
height: 25,
width: 25,
bottom: -25,
marginLeft: 30
},
text:{
marginVertical: 30,
fontSize: 20,
fontWeight: 'bold',
marginLeft: 30,
marginBottom: 10,
bottom: 5
},
});
export default Settings;
Your mistake is because of your onPress={()=> (handleLogout)}. onPress takes in a function but in your case you are making the function run another function, so your alert will not even be activated. To fix this, simply change your code to this.
onPress={handleLogout}

React Native Images FlatList Error "doesn't show the images"

enter image description here
I relied on this video, here's the github with the video code:
https://github.com/KasperKloster/ComponentsExplained/blob/main/App.js
https://www.youtube.com/watch?v=qGN3S0wO-OQ
i'm not getting to view the icon images in png and neither in svg, could they help me? I put a bacgroundColor in some random color just to see if the icons are there, and they are, only they are not showing up.. i also couldn't put logout at the bottom of the screen
Settings.js
import { SafeAreaView,Text, View, FlatList,
TouchableOpacity, StyleSheet, Image} from 'react-native';
import React, {useState} from 'react'
const Settings = () => {
const [data, setdata] = useState(DATA);
const [isRender, setisRender] = useState(false);
const DATA = [
{id: 1, text: 'Perfil', image: require('../../assets/images/user.png')},
{id: 2, text: 'Dark/Light mode', image: require('../../assets/images/light-up.png')},
{id: 3, text: 'TouchId', image: require('../../assets/images/fingerprint.png')},
{id: 4, text: 'Logout'},
]
const renderItem = ({item}) => {
return(
<TouchableOpacity
style= {styles.item}
>
<View style={ styles.avatarContainer }>
<Image loadingIndicatorSource={ item.image } style={ styles.avatar } />
</View>
<View>
<Text style={styles.text}>{item.text}</Text>
</View>
</TouchableOpacity>
)
}
return (
<SafeAreaView style={styles.container}>
<FlatList
data={DATA}
keyExtractor={(item) => item.id.toString()}
renderItem={renderItem}
extraData={isRender}
/>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 20
//marginHorizontal: 21
},
item:{
borderBottomWidth: 1,
borderBottomColor: '#808080',
alignItems: 'flex-start',
flexDirection: 'row',
},
avatarContainer: {
backgroundColor: '#A0D800',
borderRadius: 100,
height: 30,
width: 30,
justifyContent: 'center',
alignItems: 'center',
},
avatar: {
height: 50,
width: 50,
},
text:{
marginVertical: 30,
fontSize: 25,
fontWeight: 'bold',
marginLeft: 30,
marginBottom: 5
},
});
export default Settings;
change loadingIndicatorSource={ item.image } to source={ item.image }

how can I scroll the full screen with a tab view?

How can I scroll in my profil page where is a Tab and inside a Tab is a Flatlist?
Like Instagram If you go on your profile then there is a Tab and you can scroll down until no more images available.
I use this libary: https://github.com/satya164/react-native-tab-view
I tried many times and different things but I dont succeed. I hope anyone can help me
€: I can scroll but he dont show all results. And I dont want to scroll inside a Tab I want to scroll the full profile screen
<ScrollView style={styles.containerProfil}>
<View style={{flex: 1, flexDirection: 'column', alignItems: 'center', height: height * 1, width: width * 1, position: 'relative'}}>
<TouchableOpacity style={{justifyContent: 'center', alignItems: 'center', position: 'relative'}} onPress={() => navigation.navigate('Platform')}>
<ImageBackground style={styles.profilImage} resizeMode="cover" source={require('../assets/profil.jpg')} />
</TouchableOpacity>
<View style={{justifyContent: 'center', height: 40}}>
<Text style={{ fontFamily: 'nunito-regular', color: '#333', fontSize: 20, letterSpacing: 0, marginVertical: 10}}>#lenamaier123</Text>
</View>
<View style={{flex: 1, backgroundColor: '#eee', width: width * 1}}>
<ProfileTab /> <-- HERE ARE THE TABS
</View>
</View>
</ScrollView>
Css:
containerProfil: {
flex: 1,
backgroundColor: '#fff',
position: 'relative',
padding: 0,
},
profilImage: {
width: 100,
height: 100,
borderRadius: 150 / 2,
overflow: "hidden",
borderWidth: 1,
borderColor: '#eee',
textAlign: 'center',
},
ProfileTab.js
import * as React from 'react';
import { View, Text, useWindowDimensions, Dimensions } from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';
import MyProducts from '../../../screens/profile/myProducts';
const height = Dimensions.get('window').height;
const FirstRoute = () => (
<View style={{ flex: 1, backgroundColor: '#ff4081' }}>
<MyProducts />
</View>
);
const SecondRoute = () => (
<View style={{ flex: 1, backgroundColor: '#673ab7' }}>
<Text>Second</Text>
</View>
);
const renderScene = SceneMap({
first: FirstRoute,
second: SecondRoute,
});
export default function TabViewExample() {
const layout = useWindowDimensions();
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([
{ key: 'first', title: 'First' },
{ key: 'second', title: 'Second' },
]);
return (
<TabView
navigationState={{ index, routes }}
renderScene={renderScene}
onIndexChange={setIndex}
initialLayout={{ width: Dimensions.get('window').width }}
scrollEnable={true}
style={{width: layout.width * 1}}
/>
);
}
Flatlist.js
import React, { useState } from 'react';
import { StyleSheet, Text, View, TouchableOpacity, ScrollView, FlatList, SafeAreaView, Dimensions } from 'react-native';
const width = Dimensions.get('window').width;
const myProducts = () => {
const [product, setProduct] = useState([
{key: 1, text: 'Hose'},
{key: 2, text: 'Hose'},
{key: 3, text: 'Hose'},
{key: 4, text: 'Hose'},
{key: 5, text: 'Hose'},
{key: 6, text: 'Hose'},
{key: 7, text: 'Hose'},
{key: 8, text: 'Hose'},
]);
return (
<View style={{width: width * 1}}>
<FlatList
data={product}
scrollEnabled={true}
keyExtractor={(item) => item.key.toString()}
renderItem={({item}) => (<View style={{flex: 1, height: 80, width: width * 1, padding: 20, borderWidth: 1, bordderColor: '#eee'}}><Text style={{flex: 1, backgroundColor: 'red', color: 'blue'}}>{item.text}</Text></View>)}
/>
</View>
)
};
export default myProducts;
FlatList inside a ScrollView doesn't work as expected.
FlatList inside ScrollView doesn't scroll - https://github.com/facebook/react-native/issues/19971
As a solution to your problrm I found this.
you want this;
https://www.npmjs.com/package/react-native-scrollable-tab-view-collapsible-header
Take a look.
If you are using FlatList component inside tabview, change
<HScrollView> with <HFlatList>

React native border radius around sectionList

How can I put a border radius around my sectionList like in the image?
Something you can do is use the index and the number of items in the section list section to know when a border-radius is needed.
<SectionList
sections={sections}
renderItem={({ item, section, index }) => (
<ListItem
navigation={this.props.navigation}
section={section}
item={item}
index={index}
/>
)}
/>
Inside the ListItem component do the following with the index and the section that was passed through to the ListItem.
We know that the first item in our list will always have an index of 0 thus we can set the borderTopLeftRadius and Right borderTopRightRadius to 10 when the index is 0. When the index reaches the amountOfItemsInSection(it's -1 because index always starts at 0) we know that we're at the end of the list and we need to close the borders.
function ListItem({
navigation,
section
item,
index,
}) {
const amountOfItemsInSection = section.data.length - 1
return (
<View
style={{
height: 50,
flex: 1,
flexDirection: "row",
justifyContent: "space-between",
marginHorizontal: 20,
backgroundColor: "grey",
borderWidth: 1,
borderBottomWidth: index === amountOfItemsInSection ? 1 : 0,
borderColor: 'grey',
borderTopLeftRadius: index === 0 ? 10 : 0,
borderTopRightRadius: index === 0 ? 10 : 0,
borderBottomLeftRadius: index === amountOfItemsInSection ? 10 : 0,
borderBottomRightRadius: index === amountOfItemsInSection ? 10 : 0
}}
>
{/* Left */}
<View>
<Text>{heading}</Text>
<Text>{description}</Text>
</View>
</View>
)
}
Image showing the result
Example
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
SectionList,
Alert,
TouchableOpacity,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import colors from '../Component/Color';
export default class SectonLists extends Component {
GetSectionListItem = item => {
Alert.alert(item);
};
render() {
return (
<View style={styles.container}>
<SectionList
sections={[
{
title: 'City Name Starts with A',
data: ['Agra', 'Alinager', 'Amritsar'],
},
{
title: 'City Name Starts with B',
data: ['Barabanki', 'Bombay', 'Bangalore'],
},
{
title: 'City Name Starts with C',
data: ['Chakia', 'Chandauli', 'Chouk'],
},
]}
renderSectionHeader={({section}) => (
<Text style={styles.SectionHeader}> {section.title} </Text>
)}
renderItem={({item}) => (
<Text
style={styles.SectionListItemS}
onPress={this.GetSectionListItem.bind(this, item)}>
{' '}
{item}{' '}
</Text>
)}
keyExtractor={(item, index) => index}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
SectionHeader: {
backgroundColor: colors.primary,
fontSize: 20,
marginTop: 10,
padding: 5,
color: '#fff',
fontWeight: 'bold',
},
SectionListItemS: {
fontSize: 20,
padding: 6,
color: '#000',
backgroundColor: '#F5F5F5',
},
});
If you wrap your sectionList in a View with the desired borderRadius and with the overflow: 'hidden' style property then all of the content in the sectionList will be contained within a view with the desired borderRadius.
Example
<View style={{
width: '80%',
backgroundColor: 'white',
borderRadius: 10, ,
overflow: 'hidden'}} >
<SectionList
sections = {sections}
renderItem = {renderItem}
renderSectionHeader={renderSectionHeader}
keyExtractor = {(item, index) => index.toString()}
ListEmptyComponent={listEmptyComponent}
/>
</View>

Center Text in FlatList next to Icon - React Native

I am currently trying to center the text from the FlatList.
By centering I mean I want it to be in the middle, right of each Icon.
Currently this is how is displaying, it's on the bottom. I just want it to be a little bit higher:
Here is how my component looks like:
I have tried to implement few styles, but still no success.
The thing that crossed my mind but did not try was to hard code each line and drop the loop, but I am not sure if this is right to do.
import SocialIcon from 'react-native-vector-icons/AntDesign';
import BookingIcon from 'react-native-vector-icons/FontAwesome';
import FeatherIcons from 'react-native-vector-icons/Feather';
export const bookIcon = (<BookingIcon name="pencil-square-o" size={40} color="purple" />);
export const calendarIcon = (<SocialIcon name="calendar" size={40} color="purple" />);
export const questionIcon = (<SocialIcon name="questioncircleo" size={40} color="purple" />);
export const externalLinkIcon = (<FeatherIcons name="external-link" size={40} color="purple" />);
class AboutMe extends Component {
static navigationOptions = {
title: "About Me",
}
render() {
return (
<View style={styles.container}>
<View style={styles.topBox}>
<View style={styles.circleOuter} />
<View style={styles.circle} />
</View>
<View style={styles.middleBox}>
</View>
<View style={styles.bottomBox}>
<FlatList
contentContainerStyle={styles.listItem}
data={[
{key: 'Book a free appointment', page:'Book', icon: bookIcon},
{key: 'Availability', page:'Availability', icon:calendarIcon},
{key: 'FAQ', page:'Faq', icon: questionIcon},
{key: 'Useful Links', page: 'Links', icon: externalLinkIcon},
]}
onPress={() => this.props.navigation.navigate('Book')}
renderItem={({item}) => {
return (
<TouchableHighlight onPress={() => this.props.navigation.navigate(`${item.page}`)}>
<Text >{item.icon}{item.key}</Text>
</TouchableHighlight>
)
}}
/>
</View>
</View>
);
};
};
const styles = StyleSheet.create({
container: {
flex: 1
},
topBox: {
flex:3,
backgroundColor: 'red',
justifyContent:'center',
alignItems: 'center'
},
middleBox: {
flex:1,
backgroundColor: 'yellow'
},
bottomBox: {
flex:4,
backgroundColor: 'orange',
padding: 20
},
circle: {
width: 160,
height: 160,
borderRadius: 160/2,
backgroundColor: 'green',
position: 'absolute'
},
circleOuter: {
width: 180,
height: 180,
borderRadius: 180/2,
backgroundColor: 'black'
},
listItem: {
flex:1,
justifyContent: 'center'
},
text: {
fontSize: 20,
}
});
You need to wrap the Text tag in a View/TouchableHighlight tag and then center the text tag vertically. Try this and let me know. It could be necessary that you wrap the icons in a separated image tag. if the code avode dont work means that a separeted tag is necessary so let me know!
<TouchableHighlight
style={styles.buttonsStyle}
onPress={() => this.props.navigation.navigate(`${item.page}`)}
>
<Text>{item.icon}{item.key}</Text>
</TouchableHighlight>
...
...
...
//in the styles add
buttonsStyle:{
justifyContent: 'center',
}
EDIT1
In order to wrap the icons it should be like following.. Note that you cannot use TouchableHighlight in this case. it seems to be a bug with react-native. aslo i used TouchableOpacity
renderItem={({item}) => {
return (
<TouchableOpacity
style={styles.buttonsStyle}
onPress={() => this.props.navigation.navigate(`${item.page}`)}
>
<Image style={styles.imgStyles} source={item.icon} />
<Text style={styles.mappedTextStyle}>{item.key}</Text>
</TouchableOpacity>
)
}}
styles to change/add
buttonsStyle:{
alignItems: 'center',
flexDirection:'row',
marginTop: 5,
},
imgStyles:{
width: 40,
height: 40,
marginRight: 10
},
mappedTextStyle: {
fontSize: 18,
color: 'black'
},
EDIT 2
In order to cover vector-icons lib, i have created a Expo snack for producing the desired behavior. Expo snack has also the expected solution for the problem Expo Snack