My React Native modal isn't working and I can't find why - react-native

I'm not great with react native, and I'm having trouble getting a modal to pop up. It's meant to be like a warning, and then you click the button "I understand" to continue. Thanks, in the future :D
I've been following a tutorial on how to make a React Native app when I get stuck on something, but this part I wasn't able to find an answer. I'm relatively new to react native, and this is my first attempt on creating an app. It would be great to get help :)
Here's what I have so far (app.js is the only file I have yet):
import React, { useState } from 'react';
import { Button, Modal, SafeAreaView, StyleSheet, Text, TouchableOpacity, TouchableWithoutFeedback, View } from 'react-native';
export default function App() {
const [modalVisible, setModalVisible] = useState(false);
return (
<>
<SafeAreaView style={styles.container}>
<TouchableOpacity style={styles.topButton}>
<Text style={styles.text}>
Favorites
</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => setModalVisible(true)} style={styles.button}>
<Text style={styles.text}>
Memes
</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button}>
<Text style={styles.text}>
Jokes
</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button}>
<Text style={styles.text}>
Yo Mama Jokes
</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button}>
<Text style={styles.text}>
Cute Pictures
</Text>
</TouchableOpacity>
<Modal
visible={modalVisible}
animationType="fade">
style={styles.modal}
<Button title="I understand" visible={modalVisible} onPress={() => setModalVisible(false)}/>
</Modal>
<StatusBar style="auto" />
</SafeAreaView>
</>
);
}
const styles = StyleSheet.create({
container: {
flex: 10,
backgroundColor: '#0f0117',
},
topButton: {
backgroundColor: "dodgerblue",
borderRadius: 15,
alignContent: 'center',
alignItems: 'center',
paddingTop: 10,
paddingBottom: 10,
paddingLeft: 10,
paddingRight: 10,
marginTop: 10,
marginBottom: 50,
marginLeft: 10,
marginRight: 10,
},
button: {
backgroundColor: "dodgerblue",
borderRadius: 15,
alignContent: 'center',
alignItems: 'center',
paddingTop: 10,
paddingBottom: 10,
paddingLeft: 10,
paddingRight: 10,
marginTop: 10,
marginBottom: 10,
marginLeft: 10,
marginRight: 10,
},
text: {
color: '#ffffff',
fontSize: 30,
},
modal: {
backgroundColor: "#0f0117",
marginTop: 15,
marginBottom: 15,
marginLeft: 15,
marginRight: 15,
},
});**

Replace your modal code with this one
<Modal
visible={modalVisible}
animationType="fade"
style={styles.modal}>
<Button title="I understand" visible={modalVisible} onPress={() => setModalVisible(false)}/>
</Modal>
Also you forgot to import StatusBar from react-native.

It may have something to do with your styling. For styles.modal, try
modal: {
backgroundColor: "#0f0117",
marginTop: 15,
marginBottom: 15,
marginLeft: 15,
marginRight: 15,
position: "absolute"
top: "20%" # You can try different values here along with bottom, left, and right properties
},

Related

Unable to Scroll using Flatlist in React native

So i have tried couple of things, data pulled from the REST api works fine without trouble, but when i put it to Flatlist, it does not scroll. Tried countless things I saw on the internet, and still , it does not seem to work.
Below is a snippet i used, to display data from the REST api to the application :
<View>
{isLoading ? <ActivityIndicator/> : <FlatList
style={{fontFamily: 'Poppins-Medium', top: 170, left: 23}}
ItemSeparatorComponent={this.FlatListItemSeparator}
data={transaction_details}
renderItem={({item}) => (
<View style={{paddingTop: 20,flexDirection: 'row', height: 75}}>
<Image source={{uri: item.avatar}} style={{width:50, height:50, borderRadius:25,overflow:'hidden'}}/>
<Text style={styles.PayeeName}>{item.name}{" "}</Text>
<Text style={styles.date_ofTransaction}>{item.date}</Text>
<Text style={styles.amountValue}>{item.amount}</Text>
</View>
)}
keyExtractor={(item) => item.id .toString()}
scrollEnabled={true}
/>}
</View>
Looking over, i saw something like View style={{flex:1}} should I have to try this, the whole thing disappears speedily. I dont know what else I have to try hence, this.
I need help with this.
Thank you!
New Edits
done the edit you asked me to, now there are still some spaces there. Do not know how they came about,
I looks like this now so far
Here is the new Source code
import React, {useEffect, useState} from 'react';
import {
ActivityIndicator,
Image,
ImageBackground,
SafeAreaView,
StyleSheet,
Text,
View,
} from 'react-native';
import {Header, Avatar, Icon, Card} from '#rneui/themed';
import {FlatList, ScrollView} from 'react-native-gesture-handler';
import {Tab} from '#rneui/base';
const HomePage = () => {
const [transaction_details, setTransaction_details] = useState([]);
const[isLoading,setLoading] = useState(true);
const Item = ({title}) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);
FlatListItemSeparator = () => {
return (
<View
style={{
height: 1,
width: 350,
backgroundColor: '#D3D3D3',
}}
/>
);
};
useEffect(() => {
fetch('https://brotherlike-navies.000webhostapp.com/people/people.php', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
})
.then(response => response.json())
.then(responseJson => {
setTransaction_details(responseJson);
setLoading(false);
});
}, []);
return (
<View style={{flex:1, borderWidth:1}}>
<Header
containerStyle={{
backgroundColor: 'transparent',
justifyContent: 'space-around',
}}
leftComponent={
<Avatar
small
rounded
source={{
uri: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSiRne6FGeaSVKarmINpum5kCuJ-pwRiA9ZT6D4_TTnUVACpNbzwJKBMNdiicFDChdFuYA&usqp=CAU',
}}
onPress={() => console.log('Left Clicked!')}
activeOpacity={0.7}
/>
}
rightComponent={
<Icon
name={'add-circle-outline'}
color={'#00BB23'}
size={32}
onPress={() => console.log('Right Clicked!')}
/>
}></Header>
<View
style={{
flex: 1,
justifyContent: 'center',
borderadius: 9,
alignItems: 'center',
borderWidth:1
}}>
<ImageBackground
source={{
uri: 'asset:/logo/bg.JPG',
}}
imageStyle={{borderRadius: 6}}
style={{
width: 350,
height: 150,
borderadius: 9,
justifyContent: 'center',
alignItems: 'center',
}}>
<View>
<Text style={styles.accText}>Wallet Balance</Text>
<Text style={styles.text}> 250,000 </Text>
</View>
</ImageBackground>
</View>
<View style={{borderWidth:1}}>
<Text
style={{
fontFamily: 'Poppins-Bold',
flexDirection: 'row',
fontSize: 15,
left: 18,
color: 'gray',
}}>
Recent Transactions
</Text>
</View>
<View style={{flex:1, borderWidth:1}}>
{isLoading ? <ActivityIndicator/> : <FlatList
style={{fontFamily: 'Poppins-Medium', left: 23}}
ItemSeparatorComponent={this.FlatListItemSeparator}
data={transaction_details}
renderItem={({item}) => (
<View style={{flex:1,flexDirection: 'row'}}>
<Image source={{uri: item.avatar}} style={{width:50, height:50, borderRadius:25,overflow:'hidden'}}/>
<Text style={styles.PayeeName}>{item.name}{" "}</Text>
<Text style={styles.date_ofTransaction}>{item.date}</Text>
<Text style={styles.amountValue}>{item.amount}</Text>
</View>
)}
keyExtractor={(item) => item.id .toString()}
/>}
</View>
</View>
);
};
export default HomePage;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
padding: 20,
},
date_ofTransaction: {
marginTop: 20,
alignItems:'flex-start',
alignItems:'center',
left: -75,
fontFamily: 'Poppins-Light',
size: 4,
},
paragraph: {
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
padding: 20,
},
text: {
top: -85,
fontSize: 30,
color: 'white',
textAlign: 'center',
fontFamily: 'Poppins-Bold',
},
mainContainer: {
paddingTop: 90,
justifyContent: 'center',
alignItems: 'center',
},
accText: {
top: -85,
paddingTop: 10,
justifyContent: 'center',
alignItems: 'center',
fontFamily: 'Poppins-Medium',
color: 'white',
textAlign: 'center',
},
PayeeName: {
justifyContent: 'flex-start',
alignItems:'center',
left: 23,
fontFamily: 'Poppins-Medium',
size: 800,
fontWeight:'bold'
},
amountValue: {
alignItems: 'flex-end',
alignItems:'center',
right: -25,
fontFamily: 'Poppins-Medium',
size: 800,
fontWeight:'bold'
}
});
Method 1
It does not scroll because your view is restricting it. Try replacing view with <></> (Fragments).
Method 2
The other way of solving this is adding flex 1 in view container, it disappears because its parent also needs to be set to flex. So find all parents that have view in it and add flex 1 in them Then it will work

Navigating Got an invalid value for 'component' prop for the screen 'StudScreen'. It must be a valid React Component. on different screen

I just made stack navigation on tab navigation (each tab has stack navigation).
Each stack navigation has the same screens except one stack screen, so that I made and reuse the stack navigation as a functional component.
Here is my code. I think stack navigation with each component should work well depending on the input I gave.
import React from 'react';
import { SafeAreaView, StatusBar, StyleSheet, View, Text, Image, ScrollView, TextInput, Dimensions, FlatList } from 'react-native';
import COLORS from '../../consts/colors';
import Icon from 'react-native-vector-icons/MaterialIcons';
import dogs from '../../consts/dogs';
import Pressable from 'react-native/Libraries/Components/Pressable/Pressable';
const {width} = Dimensions.get ('screen');
const HomeScreen = ({navigation}) => {
const ListCategories = () => {
const[selectedCategoryIndex, setSelectedCategoryIndex]= React.useState( 0,);
const categoryList = ['Dogs For Sale', 'Upcoming Schedule' ]
return (
<View style={style.categoryListContainer}>
{categoryList.map(( category , index) =>(
<Pressable key={index} onPress={() => setSelectedCategoryIndex(index)}>
<Text
style={[
style.categoryListText,
(index == selectedCategoryIndex && style.activeCategoryListText),
]}>
{category}
</Text>
</Pressable>
))}
</View>
);
};
const ListOptions =(stud) => {
const optionsList = [
{title: 'Pets Profile', img: require('../../assets/dog1.jpg')},
{title: 'Stud Service', img: require('../../assets/dog2.jpg')},
];
return (
<Pressable onPress={() => navigation.navigate ('StudScreen', stud)}>
<View style={style.optionListContainer}>
{optionsList.map((option, index ) => (
<View style={style.optionCard} key={index}>
<Image source={option.img} style={style.optionCardImage} />
<Text style={{marginTop:10, fontSize: 18, fontWeight: 'bold'}}>
{option.title}
</Text>
</View>
))}
</View>
</Pressable>
);
};
const Card =({dog}) => {
return (
<Pressable onPress={() => navigation.navigate ('DetailsScreen', dog)}>
<View style={style.card}>
<Image source={dog.image} style={style.cardImage}/>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 10,
}}>
<Text style={{font: 16, fontWeight: 'bold'}}>{dog.breed}</Text>
<Text style={{font: 16, fontWeight: 'bold', color:COLORS.blue}}>
₱9000
</Text>
</View>
<Text style={{color:COLORS.grey, fontSize: 14, marginTop: 5}}>
{dog.description}
</Text>
<View style={{marginTop: 10, flexDirection: 'row'}}>
<View style= {style.cake}>
<Icon name="cake" size={18} />
<Text style={style.cakeText}>6 Months</Text>
</View>
</View>
</View>
</Pressable>
);
};
return (
<SafeAreaView style={{backgroundColor: COLORS.white, flex: 1}}>
<StatusBar
translucent ={false}
backgroundColor={COLORS.white}
barStyle= 'dark-content'
/>
<View style={style.header}>
<View>
<Icon name= "sort" size={30} color={COLORS.dark}/>
</View>
<Icon name= "notifications" size={30} color={COLORS.dark}/>
</View>
<ScrollView>
<View style={{
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 20,
}}>
<View style={style.searchInputContainer}>
<Icon name="search" size={25} color={COLORS.grey} />
<TextInput placeholder="Search"/>
</View>
</View>
<ListOptions/>
<ListCategories/>
<FlatList
contentContainerStyle= {{paddingLeft: 20, paddingVertical: 20}}
showsHorizontalScrollIndicator={false}
horizontal
data={dogs}
renderItem={({item}) =>
<Card dog={item} /> }/>
</ScrollView>
</SafeAreaView>
);
};
const style = StyleSheet.create ({
header: {
paddingVertical: 20,
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 20,
},
profileImage:{
height: 50,
width: 50,
borderRadius: 25,
},
searchInputContainer:{
height: 50,
backgroundColor:COLORS.light,
flex: 1,
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 20,
borderRadius: 10,
},
optionListContainer:{
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 20,
paddingHorizontal: 20,
},
optionCard:{
height: 210,
width:width/ 2 - 30,
elevation: 15,
backgroundColor: COLORS.white,
alignItems: 'center',
borderRadius: 20,
paddingTop: 10,
paddingHorizontal: 10,
},
optionCardImage:{
height: 140,
borderRadius:10,
width: '100%',
},
categoryListContainer:{
marginTop: 40,
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 40,
},
categoryListText:{
fontSize: 16,
fontWeight: 'bold',
paddingBottom: 5,
color:COLORS.grey,
},
activeCategoryListText:{
color: COLORS.dark,
borderBottomWidth: 1,
paddingBottom: 5,
},
card:{
height: 250,
backgroundColor: COLORS.white,
elevation: 10,
width:width -40,
marginRight: 20,
padding: 15,
borderRadius: 20,
},
cardImage:{
width: '100%',
height: 120,
borderRadius: 15,
},
cake:{
flexDirection: 'row',
marginRight: 15,
},
CakeText:{
marginLeft: 5,
color:COLORS.grey,
},
});
export default HomeScreen;

React Native scrollview stops scrolling after screen change

I have a ScrollView in react native where the functionality stops working only after I change the screen. For example: when I reload and land on the home screen (with the ScrollView) it all works fine. but when I use the bottom tab navigator to change the screen and return back to the home screen, it doesn't work anymore.
Thanks in advance for the help!
const Home = () => {
return (
<SafeAreaView style={{ flex: 1, backgroundColor: COLORS.background }}>
<Header screenName="home" />
<View style={{ color: COLORS.secondary, flex: 14, paddingHorizontal: SIZES.width / 20}}>
<ScrollView
showsVerticalScrollIndicator={false}
>
<Text style={styles.popularHeader}>Title</Text>
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
style={styles.popular}
>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
<View style={styles.popularItem}>
<Text>Name</Text>
</View>
</ScrollView>
<View style={styles.event}>
<Text style={styles.eventTitle}>event title</Text>
<Text style={styles.eventDate}>date</Text>
</View>
</ScrollView>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
popularHeader: {
color: COLORS.text,
fontSize: SIZES.h4,
alignSelf: 'center',
marginBottom: 10
},
popular: {
borderBottomWidth: 1,
paddingBottom: SIZES.height / 30,
marginBottom: SIZES.height / 30,
borderBottomColor: COLORS.secondary
},
popularItem: {
width: 100,
height: 100,
borderRadius: 5,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: COLORS.text,
marginRight: 10,
},
event:{
height: SIZES.height / 3,
backgroundColor: COLORS.text,
borderRadius: 5,
marginBottom: SIZES.height / 30,
padding: 10
},
eventTitle: {
fontSize: SIZES.h3,
alignSelf: 'center'
},
eventDate: {
fontSize: SIZES.h6,
alignSelf: 'center'
}
})
In case someone wonders, after a three-day-long search I found out the problem was with the bottomTabNavigator not unmounting the screens after a change of screen. so the solution that fixed my problem is:
in screenOptions:
unmountOnBlur: true

React Native change integer value when button is kicked

I am having a problem changing an integer value, the code below is showing a list of item, and control how many items will be shown when the button is clicked
The variable I want to change when it is clicked,
var showdata = 5;
code for the button and want to change it to 10 when clicked, also want the text change to Less, and showdata change back to 5 when it is for the second time
<View style={externalStyle.more_buttom}>
<TouchableOpacity onPress={() => showdata == 10}>
<Text
style={{
fontWeight: "bold",
fontSize: 13,
color: "#FFF",
}}
>
More{" "}
</Text>
</TouchableOpacity>
</View>
code for the list of item, show data used at "topTen.slice"
<ScrollView
vertical
showsHorizontalScrollIndicator={false}
style={{ paddingBottom: 0 }}
>
{isLoadingTopTen ? (
<Text
style={{
fontWeight: "bold",
fontSize: 13,
color: "#FFF",
textAlign: "center",
}}
>
Loading top ten
</Text>
) : (
topTen
.slice(0, showdata)
.map((item, index) => (
<InfoCard
navigation={navigation}
brand={item.Name}
amount={item.esgrating}
key={index}
/>
))
)}
</ScrollView>
Use useState hooks for changing the value of showData.
const [showdata, setShowData] = useState(5)
//..........
<View style={externalStyle.more_buttom}>
<TouchableOpacity onPress={() => setShowdata(10)}>
<Text
style={{
fontWeight: "bold",
fontSize: 13,
color: "#FFF",
}}
>
More{" "}
</Text>
</TouchableOpacity>
</View>
Working Example: Expo Snack
import React, { useState } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import Constants from 'expo-constants';
export default function App() {
const [showdata, setShowData] = useState(5);
return (
<View style={styles.container}>
<TouchableOpacity
onPress={() => {
setShowData(10);
}}>
<View style={{ backgroundColor: 'pink', padding: 10 }}>
<Text
style={{
fontWeight: 'bold',
fontSize: 13,
color: '#000',
}}>
More {showdata}
</Text>
</View>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
});

React-native TouchableOpacity button doesn't respect border/border-radius

I have created a generic button which I'd like to have round edges instead of being a square. My button component is as such:
const Button = ({ onPress, children }) => {
const { buttonStyle, textStyle } = styles;
return (
<TouchableOpacity onPress={onPress} style={buttonStyle}>
<Text style={textStyle}>
{children}
</Text>
</TouchableOpacity>
);
};
const styles = {
textStyle: {
alignSelf: 'center',
color: colors.primaryTeal,
fontSize: 16,
fontWeight: '600',
paddingTop: 10,
paddingBottom: 10,
},
buttonStyle: {
flex: 1,
backgroundColor: colors.whiteText,
marginLeft: 5,
marginRight: 5,
borderRadius: 50
}
};
However, it remains to be square and doesn't respond to borderRadius at all.
It's invoked as such:
<Button onPress={this.onButtonPress.bind(this)}>
Log in
</Button>
How do I make it respect borderRadius and get round edges?
The login form in which it appears(Render)
render() {
return (
<Card>
<CardSection>
<Input
placeholder="user#gmail.com"
label="Email"
value={this.state.email}
onChangeText={email => this.setState({ email })}
/>
</CardSection>
<CardSection>
<Input
secureTextEntry
placeholder="password"
label="Password"
value={this.state.password}
onChangeText={password => this.setState({ password })}
/>
</CardSection>
<View style={styles.btnWrapper}>
<CardSection>
{this.state.loading ?
<Spinner size="small" /> :
<Button onPress={this.onButtonPress.bind(this)}>
Log in
</Button>}
</CardSection>
</View>
<Text style={styles.errorTextStyle} disabled={this.state.error !== null}>
{this.state.error}
</Text>
</Card>
CardSection component:
const CardSection = (props) => {
return (
<View style={styles.containerStyle}>
{props.children}
</View>
);
};
const styles = {
containerStyle: {
borderBottomWidth: 1,
padding: 5,
backgroundColor: '#fff',
justifyContent: 'flex-start',
flexDirection: 'row',
borderColor: '#ddd',
position: 'relative'
}
};
Works perfectly fine. Just make sure to use react native's StyleSheet.create to get cached styles.
Maybe your button container view background is white and you're not seeing the rounded corners.
Heres my working snack
Code snippet i used, but refer to the snack as you'll see it live in action.
import React, { Component } from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
const Button = ({ onPress, children }) => {
return (
<TouchableOpacity onPress={onPress} style={styles.buttonStyle}>
<Text style={styles.textStyle}>
{children}
</Text>
</TouchableOpacity>
);
};
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Button>
Log in
</Button>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex:1,
backgroundColor: 'black',
},
textStyle: {
alignSelf: 'center',
color: 'teal',
fontSize: 16,
fontWeight: '600',
paddingTop: 10,
paddingBottom: 10,
},
buttonStyle: {
flex: 1,
backgroundColor: 'white',
marginLeft: 5,
marginRight: 5,
borderRadius: 50
},
});
You have to add style overflow: hidden to TouchableOpacity
Try add attribute borderWidth and borderColor on buttonStyle, like below:
buttonStyle: {
backgroundColor: colors.whiteText,
marginLeft: 5,
marginRight: 5,
borderRadius: 50,
borderWidth: 2,
borderColor: "#3b5998"
}
A complete example:
<TouchableOpacity
onPress={() => handleSubmit()}
disabled={!isValid}
style={{
alignSelf: "center",
marginBottom: 30,
overflow: 'hidden',
borderColor: "#3b5998",
borderWidth: 2,
borderRadius: 100,
}}
>
<View
style={{
flexDirection: "row",
alignSelf: "center",
paddingLeft: 15,
paddingRight: 15,
minHeight: 40,
alignItems: 'center',
}}
>
<Text style={{ fontSize: 20, fontWeight: "bold", color: "#3b5998" }}>
SEND
</Text>
</View>
</TouchableOpacity>
I think you might be looking for the containerStyle prop
<TouchableOpacity containerStyle={ViewStyleProps}>