How can I show some components when i click on a button? - reactjs

next to Entertain text, there is an icon, called Button,
When I click on that icon, I want those parts(netflix to twitch) to come that place.
How can I do that? I mean, i want those part to be hide at first, but after i click on that icon, it should come to that place.
Here are my code,
ThirdScreen.js
import React from "react";
import { Text, SafeAreaView, View, StyleSheet, ScrollView } from "react-native";
import Transfer from "../icons/transfer.svg";
import Entertain from "../icons/entertain.svg";
import Netflix from "../icons/netflix.svg";
import Button from "../icons/button.svg";
import Dribble from "../icons/dribble.svg";
import Spotify from "../icons/spotify.svg";
import Twitch from "../icons/twitch.svg";
import Bill from "../icons/bill.svg";
import Time from "../icons/time.svg";
import Home from "../icons/home.svg";
import User from "../icons/user.svg";
import Ratio from "../icons/ratio.svg";
const ThirdScreen = () => {
return (
<SafeAreaView style={{ flex: 1, backgroundColor:"#ffffff"}}>
<View style={{ alignItems: "center", padding: 20 }}>
<Text style={styles.transaction}>Transaction History</Text>
</View>
<View style={{ padding: 10, }}>
<View style={styles.rows}>
<Text style={styles.bigBold}>Category</Text>
<Text style={{ color: "#7cb3d9" }}>This week</Text>
</View>
<View style={{ padding: 20 }}>
{/* Transfer */}
<View style={[styles.elevation, {marginBottom:10,}]}>
<View style={[styles.rows, {padding:5}]}>
<View style={styles.iconText}>
<Transfer height={30} width={22} fill={"#1E2439"} />
<Text style={styles.bigBold}>Transfer</Text>
</View>
<View>
<Button height={30} width={22} fill={"#1E2439"} />
</View>
</View>
</View>
{/* Entertain */}
<View style={styles.elevation}>
<View style={[styles.rows,{padding:5,}]}>
<View style={styles.iconText}>
<Entertain height={30} width={22} fill={"#1E2439"} />
<Text style={styles.bigBold}>Entertain</Text>
</View>
<View>
<Button height={30} width={22} fill={"#1E2439"} />
</View>
</View>
</View>
<ScrollView style={{padding:10}}>
{/* Netflix */}
<View style={styles.rows}>
<View style={styles.iconText}>
<View style={[styles.iconBackground, { backgroundColor: "#ffd4d5"}]}>
<Netflix height={30} width={22} fill={"#1E2439"} />
</View>
<View style={styles.entertainItems}>
<Text style={styles.entertainItemsText}>Netflix</Text>
<Text style={styles.grayText}>4 March</Text>
</View>
</View>
<View style={{ flexDirection: "column", alignItems: "flex-end" }}>
<Text style={styles.redText}>-$8.99</Text>
<Text style={styles.grayText}>Subscription</Text>
</View>
</View>
{/* Dribble */}
<View style={styles.rows}>
<View style={styles.iconText}>
<View style={[styles.iconBackground, { backgroundColor: "#fdd7e6" }]}>
<Dribble height={30} width={22} fill={"#1E2439"} />
</View>
<View style={styles.entertainItems}>
<Text style={styles.entertainItemsText}>Dribble</Text>
<Text style={styles.grayText}>12 March</Text>
</View>
</View>
<View style={{ flexDirection: "column", alignItems: "flex-end" }}>
<Text style={styles.redText}>-$120.00</Text>
<Text style={styles.grayText}>Subscription</Text>
</View>
</View>
{/* Spotify */}
<View style={{ marginVertical: 10, flexDirection: "row", justifyContent: "space-between" }}>
<View style={styles.iconText}>
<View style={[styles.iconBackground, { backgroundColor: "#d5ffd9" }]}>
<Spotify height={30} width={22} fill={"#1E2439"} />
</View>
<View style={styles.entertainItems}>
<Text style={styles.entertainItemsText}>Spotify</Text>
<Text style={styles.grayText}>25 March</Text>
</View>
</View>
<View style={{ flexDirection: "column", alignItems: "flex-end" }}>
<Text style={styles.redText}>-$9.99</Text>
<Text style={styles.grayText}>Subscription</Text>
</View>
</View>
{/* Twitch */}
<View style={{ marginVertical: 10, flexDirection: "row", justifyContent: "space-between" }}>
<View style={styles.iconText}>
<View style={[styles.iconBackground, { backgroundColor: "#efd6ff" }]}>
<Twitch height={30} width={22} fill={"#1E2439"} />
</View>
<View style={styles.entertainItems}>
<Text style={styles.entertainItemsText}>Twitch</Text>
<Text style={styles.grayText}>25 March</Text>
</View>
</View>
<View style={{ flexDirection: "column", alignItems: "flex-end" }}>
<Text style={styles.redText}>-$24.99</Text>
<Text style={styles.grayText}>Subscription</Text>
</View>
</View>
</ScrollView>
{/* Bill */}
<View style={styles.elevation}>
<View style={[styles.rows, { padding: 5, }]}>
<View style={styles.iconText}>
<Bill height={30} width={22} fill={"#1E2439"} />
<Text style={styles.bigBold}>Bill</Text>
</View>
<View>
<Button height={30} width={22} fill={"#1E2439"} />
</View>
</View>
</View>
</View>
{/* Footer */}
<View style={{flexDirection:"row", alignItems:"center", justifyContent:"space-between",marginHorizontal:35,}}>
<Home height={30} width={22} fill={"#1E2439"} />
<Ratio height={30} width={22} fill={"#1E2439"} />
<Time height={30} width={22} fill={"#1E2439"} />
<User height={30} width={22} fill={"#1E2439"} />
</View>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
transaction: {
fontSize: 17,
fontWeight: "bold",
letterSpacing: .5,
},
elevation: {
backgroundColor: "#fdfdfd",
elevation: 3,
height: 60,
borderRadius: 10,
},
rows: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
marginVertical:10,
},
iconText: {
flexDirection: "row",
alignItems: "center",
},
iconBackground: {
height: 40,
width: 40,
borderRadius: 13,
alignItems: "center",
justifyContent: "center"
},
entertainItems: {
flexDirection: "column",
marginLeft:20,
},
bigBold: {
fontSize: 17,
fontWeight: "bold",
marginLeft:15,
},
entertainItemsText: {
fontSize:15,
fontWeight:"bold",
},
redText : {
color:"#e46e6a",
fontWeight: "bold",
fontSize:15,
},
grayText: {
color: "#b2b4c3",
},
})
export default ThirdScreen;
Here is the screenshot:

A simple approach is to store a boolean isShown that is toggled when interacting with the button. Use the useState hook to store and update that boolean:
const ThirdScreen = () => {
// Default to `false` so the section is initially hidden
const [isShown, setIsShown] = React.useState(false);
return (
<SafeAreaView style={{ flex: 1, backgroundColor:"#ffffff"}}>
<View style={{ alignItems: "center", padding: 20 }}>
<Text style={styles.transaction}>Transaction History</Text>
</View>
<View style={{ padding: 10, }}>
{/* ... */}
<View style={{ padding: 20 }}>
{/* Transfer */}
{/* ... */}
{/* Entertain */}
<View style={styles.elevation}>
<View style={[styles.rows,{padding:5,}]}>
<View style={styles.iconText}>
<Entertain height={30} width={22} fill={"#1E2439"} />
<Text style={styles.bigBold}>Entertain</Text>
</View>
<View>
<Button
height={30}
width={22}
onClick={() => { setIsShown((currIsShown) => !currIsShown); }}
fill={"#1E2439"}
/>
</View>
</View>
</View>
{/* Render the inner <ScrollView> only if `isShown` is true */}
{isShown && (
<ScrollView style={{padding:10}}>
{/* Netflix */}
{/* ... */}
</ScrollView>
)}
{/* Bill */}
{/* ... */}
</View>
{/* Footer */}
{/* ... */}
</View>
</SafeAreaView>
)
}

You can use the onClick event to change your required dom
For this first, make a state and set the initial value to false. Then use the onClick event and change the state using useEffect of setState.
At Last, change the CSS of the section to display: "none" on false and display: "block"( or other value ) on true

Related

Why are my buttons disappearing in React Native?

So I am trying to have two buttons on my React Native project and whenever I add text to my code the buttons disappear. Does anyone know why? The text I am trying to add is below
<Text
style={{
fontSize: 55,
fontFamily: 'Futura',
color: '#707070',
textAlign: 'left',
paddingTop: 50,
paddingLeft: 30
}}
>
Feed
</Text>
<Text
style={{
fontSize: 22,
fontFamily: 'Futura',
color: '#BABABA',
textAlign: 'left',
paddingTop: 10,
paddingLeft: 30
}}
>
Find the Circle for your needs!
</Text>
and the code I am trying to add it to is this
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
</View>
All of my code is below but it only shows the text not the buttons.
import React, { Component } from 'react';
import { Button, View, StyleSheet, Text } from 'react-native';
export default class GridView extends Component {
render() {
return (
<View>
<Text
style={{
fontSize: 55,
fontFamily: 'Futura',
color: '#707070',
textAlign: 'left',
paddingTop: 50,
paddingLeft: 30
}}
>
Feed
</Text>
<Text
style={{
fontSize: 22,
fontFamily: 'Futura',
color: '#BABABA',
textAlign: 'left',
paddingTop: 10,
paddingLeft: 30
}}
>
Find the Circle for your needs!
</Text>
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
buttonContainer: {
flex: 1,
},
headers: {
fontSize: 50,
}
});
This should fix your issue: My guess is there is something wrong with a parent component that is cutting of the render. If this is the only view on page also try adding the style {flex:1} to the outer most layer. This can happen sometimes if there is some styling on navigation setup that get passed to childern.
<View style={{flex:1}>
<Text style={{...}}>Feed</Text>
<Text style={{...}}>Find the Circle for your needs!</Text>
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Button 1"/>
</View>
<View style={styles.buttonContainer}>
<Button title="Button 2"/>
</View>
</View>
</View>

react native button does not fit on the screen

I have 7 buttons on the screen, but 1 button is half visible, one button does not fit at all.
my code:
render() {
return (
<ScrollView style={styles.container}>
<Header />
<View>
<Text style={styles.headerText}>
LC Teknoloji
</Text>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Isgr}
title="İş Giriş"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Yapilicak}
title="Yapılacaklar"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.gcms}
title="İş Geçmişi"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.alnck}
title="Alınıcaklar"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.ulist}
title="Ürün Listesi"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Stok}
title="Stoklar"
color="#00B0FF"
/>
</View>
<View style={styles.btnstyl}>
<Button
onPress={this.Musteri}
title="Müşteriler"
color="#00B0FF"
/>
</View>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'grey',
flex: 1,
backgroundColor: "#F5FCFF",
},
headerText: {
fontSize: 20,
textAlign: "center",
margin: 0,
fontWeight: "bold",
top: 45,
},
btnstyl: {
left: 120,
height: 60,
width: 160,
marginBottom: -20,
top: 55,
},
})
enter image description here
I changed the background color or something, when I pull it up, the buttons come up, it doesn't fit on the screen from the place in the picture
Why do you think the screenshot doesn't fit?
In your styles. Remove the marginBottom and top
const styles = {
.....
btnstyl: {
left: 120,
height: 60,
width: 160
},
}
And add height to the View component of the text above the buttons:
<View style={{ height: 100 }}>
<Text style={styles.headerText}>
LC Teknoloji
</Text>
</View>

FlattList not scrolling all content

I'm learning react native and decided to make a list of my games, for that I use a FlatList (code below), but for some reason the FlatList just scroll a little and I can't see the entire content. I've tryed to add 'flex: 1' in a style (like some awnsers in the internet) but when I do this, all my content turn blank. Here are a screenshot of the maximum I can scroll.
My code (The function don't change nothing in layout, I just pick some data from a API, so I don't paste here):
import React from 'react';
import { Button, FlatList, Image, SafeAreaView, StatusBar, StyleSheet, Text, TextInput, View } from 'react-native'
export default function App() {
const [id, setId] = React.useState('');
const [apps, setApps] = React.useState([]);
return (
<SafeAreaView style={{padding: 20}}>
<StatusBar hidden={false} backgroundColor={"white"} barStyle={'dark-content'} />
<View>
<TextInput style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} onChangeText={text => setId(text)} keyboardType={'numeric'} value={id} placeholder={"Insira o ID do jogo aqui"} />
<Button onPress={e => addApp()} title="Adicionar" color="#841584" accessibilityLabel="Salvar Id" />
<View style={{marginTop: 32}}>
<FlatList data={apps} scrollEnabled={true}
renderItem={({item}) =>
<View style={{alignItems: 'center', marginBottom: 32}}>
<Image style={{width: "100%", height: 200}} source={{ uri: item.image}} />
<Text style={{fontSize: 20}}>{item.name}</Text>
<View style={{width: '100%', flexDirection: 'row', flex: 4, justifyContent: 'space-around'}}>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/disc.png')} />
<Text>{item.disc}</Text>
</View>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/calendar.png')} />
<Text>{item.dateLaunch}</Text>
</View>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/calendar.png')} />
<Text>{item.dateBuy}</Text>
</View>
<View style={{flexDirection:'row', alignItems: 'center'}}>
<Image style={{width: 15, height: 15, marginRight: 5}} source={require('./assets/coin.png')} />
<Text style={{fontWeight: 'bold'}}>{item.price}</Text>
</View>
</View>
</View>
}
/>
</View>
</View>
</SafeAreaView>
);
}
Try this:
<FlatList style={{ flex: 0 }}
initialNumToRender={apps.length}
Or try:
Try using flex: 1 in the listContainer styling, this should make it stay in the boundaries of your parent view.

how to style text component to the right of view?

Hi, I want the date shown above to be on the right side of the view, however am unable to do it, could anyone suggest anything?
My code:
<View style={{padding: 10}}>
{props.data.data.content != undefined &&
props.data.data.content != null &&
props.data.data.content.map((item) => {
return (
<View
style={{
flexDirection: 'row',
padding: 10,
backgroundColor: '#fff',
elevation: 3,
margin: '2%',
borderRadius: 5,
}}>
<View>
<Image
source={require('../assets/atbirth.jpg')}
style={{height: 40, width: 50}}
resizeMode="contain"
/>
</View>
<View>
<View style={{flexDirection: 'row'}}>
<Text
key={item.id}
style={{
fontFamily: 'Roboto',
fontSize: 18,
fontWeight: 'bold',
}}>
{item.name}
</Text>
<View >
<Text style={{color: 'grey', fontSize: 12,}}>
{item.display_date}
</Text>
</View>
</View>
<View style={{flexDirection: 'row'}}>
{item.vaccine_list.map((i) => {
return (
<View style={{flexDirection: 'row'}}>
<Text style={{fontFamily: 'Roboto', fontSize: 15}}>
{i.name},
</Text>
</View>
);
})}
</View>
</View>
</View>
);
})}
</View>
Let me know, where am going wrong and how do I implement the above shown behavior, any suggestions would be great.
You can align your elements by using Flex.
<View style={{padding: 10}}>
{props.data.data.content != undefined &&
props.data.data.content != null &&
props.data.data.content.map((item) => {
return (
<View
style={{
flexDirection: 'row',
padding: 10,
backgroundColor: '#fff',
elevation: 3,
margin: '2%',
borderRadius: 5,
}}>
<View>
<Image
source={require('../assets/atbirth.jpg')}
style={{height: 40, width: 50}}
resizeMode="contain"
/>
</View>
<View style={{flex:1}}>
<View style={{flexDirection: 'row', flex: 1}}>
<Text
key={item.id}
style={{
fontFamily: 'Roboto',
fontSize: 18,
fontWeight: 'bold',
}}>
{item.name}
</Text>
<View style={{alignItems: 'flex-end'}}>
<Text style={{color: 'grey', fontSize: 12,}}>
{item.display_date}
</Text>
</View>
</View>
<View style={{flexDirection: 'row'}}>
{item.vaccine_list.map((i) => {
return (
<View style={{flexDirection: 'row'}}>
<Text style={{fontFamily: 'Roboto', fontSize: 15}}>
{i.name},
</Text>
</View>
);
})}
</View>
</View>
</View>
);
})}
</View>
Using Flex you are going to take all the available space and then you will be able to align the date to the right

Weird Gap Between Views in React Native

I am new to react naive and am trying to place two views one under the other but when I try doing this there is a big gap between the views as shown below.
This anyway to be able to fix this or do I need to use flatlist?
Here is my code.
render() {
return (
<>
<View style={{ flex: 1, flexDirection: "row", height: 130 }}>
<View style={styles.IconImage}>
<TouchableOpacity
onPress={() =>
Linking.openURL("http://facebook.com/")
}
>
<FontAwesome
name="location-arrow"
size={40}
color={"#E8AA65"}
/>
</TouchableOpacity>
</View>
<View style={{ paddingTop: 50, paddingLeft: 40 }}>
<Text style={{ fontSize: 20 }}>Find Us</Text>
</View>
</View>
<View style={{ flexDirection: "row", height: 130 }}>
<View style={styles.IconImage}>
<TouchableOpacity
onPress={() =>
Linking.openURL("http://facebook.com/")
}
>
<Icon
name={Platform.OS === "ios" ? "ios-settings" : "md-settings"}
size={40}
color={"#E8AA65"}
/>
</TouchableOpacity>
</View>
<View style={{ paddingTop: 50, paddingLeft: 40 }}>
<Text style={{ fontSize: 20 }}>Settings</Text>
</View>
</View>
</>
);
}
const styles = StyleSheet.create({
IconImage: {
paddingTop: 40,
paddingLeft: 40,
},
});
The issue is caused by providing flex:1 in your main view.
Output without flex:1:
The remaining offset is caused by your height together with your padding values.
Demo:
I've created a snack where you can play around with it:
https://snack.expo.io/rkUKpLUUU

Resources