How to do a loop for cards - reactjs

how do i do a loop for 10 cards? Cant find any solution anywhere.
I need it to output cards with some info which ill specify later. I am new to React Native and got no idea what Im doing. It worked when ive written return into the loop, but it returned just one card (better than none I guess).
import * as React from 'react';
import { StyleSheet, ScrollView, View, Text, Image } from 'react-native';
import { Card, CardTitle, CardContent, CardAction, CardButton, CardImage } from 'react-native-material-cards'
export default function Primary({ navigation })
{
return(
<ScrollView style=
{{
flex: 1,
}}>
<View>
<Text style={{ fontSize: 26, fontWeight: 'bold' }}>
Hlavni
</Text>
<Text>Ahojda</Text>
</View>
<Text>Swag</Text>
<KartyLoop/>
</ScrollView>
);
}
const KartyLoop = () => {
var swag[];
for (i=0; i<5; i++)
{
<View>
<Card style={{borderRadius: 25}}>
<CardTitle
title="This is a title"
subtitle="This is subtitle"
/>
<CardContent text="Your device will reboot in few seconds once successful, be patient meanwhile" />
<CardAction
separator={true}
inColumn={false}>
<CardButton
onPress={() => {}}
title="Push"
color="blue"
/>
<CardButton
onPress={() => {}}
title="Later"
color="blue"
/>
</CardAction>
</Card>
</View>
}
return();
}

When rendering a list of components in react native, you need to put them in some sort of list.
Try wrapping the code inside your <View> with <Flatlist>
Docs: https://reactnative.dev/docs/flatlist
Full example from docs:
import React from 'react';
import { SafeAreaView, View, FlatList, StyleSheet, Text, StatusBar } from 'react-native';
const DATA = [
{
id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba',
title: 'First Item',
},
{
id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63',
title: 'Second Item',
},
{
id: '58694a0f-3da1-471f-bd96-145571e29d72',
title: 'Third Item',
},
];
const Item = ({ title }) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);
const App = () => {
const renderItem = ({ item }) => (
<Item title={item.title} />
);
return (
<SafeAreaView style={styles.container}>
<FlatList
data={DATA}
renderItem={renderItem}
keyExtractor={item => item.id}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: StatusBar.currentHeight || 0,
},
item: {
backgroundColor: '#f9c2ff',
padding: 20,
marginVertical: 8,
marginHorizontal: 16,
},
title: {
fontSize: 32,
},
});
export default App;

Related

How to use Scrollview with KeyboardAvoidingView

I am a new to react native and i wonder how to use scrollView with keyboardAvoid in the following scene.I have searched for some relevant information, but still have no clue.
Following picture may be more intuitive then explain.
enter image description here
import React,{useRef,useEffect,useState} from 'react';
import { Text, View, StyleSheet,TextInput,ScrollView,KeyboardAvoidingView,TouchableOpacity } from 'react-native';
export default function App() {
const [selectedItem,setSeLectedItem] = useState(null)
const [userTyping,setUserTyping] = useState(false);
const selected = (index) => {
setUserTyping(true)
setSeLectedItem(index)
}
const onBlur=()=>{
setUserTyping(false)
}
return (
<View style={styles.container}>
<KeyboardAvoidingView style={{flex:1}}>
<ScrollView style={styles.scroll} >
{
['1', '2', '3', '4','5','6','7'].map((item,index) => (
<TouchableOpacity onPress={()=>selected(index)} key={item}>
<View style={[styles.itemWrapper,selectedItem===index &&styles.selectedItem]}>
<Text style={styles.itemText}>TEST {item}</Text>
{
(selectedItem===index)&&userTyping&&
<InputFC
style={styles.itemInput}
placeholder='NO TEXT'
placeholderTextColor={'white'}
autoCapitalize={'none'}
onBlur={onBlur}
/>
}
</View>
</TouchableOpacity>
))
}
</ScrollView>
</KeyboardAvoidingView>
</View>
);
}
const InputFC = (props) => {
const {style,placeholder,placeholderTextColor,autoCapitalize,onBlur} = props
const inputRef = useRef(null);
useEffect(()=>{
if(inputRef.current) inputRef?.current?.focus()
})
return (
<TextInput
style={style}
ref={inputRef}
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
autoCapitalize={autoCapitalize}
onBlur={onBlur}
/>)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#24292e',
},
scroll: {
flex: 1,
paddingHorizontal: 20,
},
itemWrapper:{
width: '100%',
paddingLeft:20,
paddingVertical:20,
marginBottom: 20,
backgroundColor: '#6b6965',
borderRadius:20,
},
itemText:{
fontSize:20,
fontWeight:'bold',
color:'white',
alignItems:'center',
justifyContent:'center',
marginBottom:5,
}
,
itemInput:{
fontSize:20,
color:'white',
},
selectedItem:{
borderWidth:3,
borderColor:'#2188ff'
}
});
Simplified Demo online:
https://snack.expo.dev/#bravo20203/demo-about-scrollview
Thanks.
You can make this work by changing the flex: 1 to different components and by adding
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={{flex: 1}}
to the KeyboardAvoidingView. I have also wrapped your content into a SafeAreaView so it does not go into the header on iOS.
Since you are dynamically expanding the TextInput on press, which changes its height, I have added a keyboardVerticalOffset to the KeyboardAvoidingView and made the overflow visible.
The final code is as follows.
import React, { useEffect, createRef, useState, useRef } from 'react';
import { useHeaderHeight } from "#react-navigation/elements";
import {
Text,
View,
KeyboardAvoidingView,
ScrollView,
TextInput,
Platform,
TouchableOpacity,
StyleSheet,
SafeAreaView
} from 'react-native';
export default function App() {
const [selectedItem,setSeLectedItem] = useState(null)
const [userTyping,setUserTyping] = useState(false);
const selected = (index) => {
setUserTyping(true)
setSeLectedItem(index)
}
const onBlur=()=>{
setUserTyping(false)
}
return (
<SafeAreaView style={styles.container}>
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={Platform.OS === 'ios' && {flex: 1}}
keyboardVerticalOffset={30}
>
<ScrollView style={styles.scroll}>
{
['1', '2', '3', '4','5','6','7'].map((item,index) => (
<TouchableOpacity onPress={()=>selected(index)} key={item}>
<View style={[styles.itemWrapper,selectedItem===index &&styles.selectedItem]}>
<Text style={styles.itemText}>TEST {item}</Text>
{
(selectedItem===index)&&userTyping&&
<InputFC
style={styles.itemInput}
placeholder='NO TEXT'
placeholderTextColor={'white'}
autoCapitalize={'none'}
onBlur={onBlur}
/>
}
</View>
</TouchableOpacity>
))
}
</ScrollView>
</KeyboardAvoidingView>
</SafeAreaView>
);
}
const InputFC = (props) => {
const {style,placeholder,placeholderTextColor,autoCapitalize,onBlur} = props
const inputRef = useRef(null);
useEffect(()=>{
if(inputRef.current) inputRef?.current?.focus()
})
return (
<TextInput
style={style}
ref={inputRef}
placeholder={placeholder}
placeholderTextColor={placeholderTextColor}
autoCapitalize={autoCapitalize}
onBlur={onBlur}
/>)
}
const styles = StyleSheet.create({
container: {
backgroundColor: '#24292e',
flex: 1
},
scroll: {
paddingHorizontal: 20,
overflow: "visible",
},
itemWrapper:{
width: '100%',
paddingLeft:20,
paddingVertical:20,
marginBottom: 20,
backgroundColor: '#6b6965',
borderRadius:20,
},
itemText:{
fontSize:20,
fontWeight:'bold',
color:'white',
alignItems:'center',
justifyContent:'center',
marginBottom:5,
}
,
itemInput:{
fontSize:20,
color:'white',
},
selectedItem:{
borderWidth:3,
borderColor:'#2188ff',
}
});
I have updated your snack here. Notice that I have tested this on iOS only.
If the keyboard is opened on select, the selected Input is now fully visible.

making a virtualized list with a separate file in react

I created a to do list app in app.js and a virtualized list in VirtualizedList.js but none of the virtualized list code is displaying although I imported it into app.js properly.
Here is the VirtualizedList.js code.
import React from 'react';
import { SafeAreaView, View, VirtualizedList, StyleSheet, Text,
StatusBar } from 'react-native';
const DATA = [];
const getItem = (data, index) => ({
id: Math.random().toString(12).substring(0),
title: `Item ${index+1}`
});
const getItemCount = (data) => 50;
const Item = ({ title }) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);
const App = () => {
return (
<SafeAreaView style={styles.container}>
<VirtualizedList
data={DATA}
initialNumToRender={4}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={item => item.key}
getItemCount={getItemCount}
getItem={getItem}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: StatusBar.currentHeight,
},
item: {
backgroundColor: '#f9c2ff',
height: 100,
justifyContent: 'center',
marginVertical: 7,
marginHorizontal: 24,
padding: 20,
},
title: {
fontSize: 25,
},
});
export default App;
This is how I imported VirtualizedList into app.js:
import VirtualizedList from '/Users/myname/Applications/todo-
app/src/components/VirtualizedList';
Normally you would export default your list in VirtualizedList.js and you would import them in your App.js (renderer) as a normal object. But I would have to see the code to be sure

i am trying to add touchableOpacity for my flatlist but it isn't working so I used button so now I am getting this error

My code :
import React from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
Pressable,
TouchableOpacity,
FlatList,
Button
} from 'react-native';
// import {Card} from 'react-native-material-cards';
import {Card} from 'react-native-paper';
import moment from 'moment';
export default function ScreenA({navigation}) {
const [timeSlots,setTimeSlots]=React.useState([]);
const createTimeSlots =(fromTime,toTime)=>{
let starTime = moment(fromTime,'hh:mm A' );
let endTime = moment(toTime,'hh:mm A' );
if(endTime.isBefore(starTime)){
endTime.add(1,'day');
}
let arr=[];
while(starTime<=endTime){
arr.push(new moment(starTime).format('hh:mm A' ));
starTime.add(30,'minutes');
}
return arr;
}
React.useEffect(() => {
setTimeSlots(createTimeSlots('09:00 AM','05:00 PM'))
// let slots = createTimeSlots('09:00','17:00');
// console.log(slots);
}, [])
const onPressHandler = ()=>{
navigation.navigate('Screen_N');
}
const DATA = timeSlots.map((main,index)=>{
return{
key:main
}
})
return(
<View>
<Text style={[styles.bigBlue]}>Screen_Assss</Text>
<FlatList
style={styles.cards}
contentContainerStyle={{paddingBottom: 45,}}
data={DATA}
keyExtractor={(item) => item.key}
renderItem={({item,index}) => {
return <SafeAreaView style={{flexDirection:'row',padding: 10,marginBottom:10,backgroundColor:'blue',borderRadius: 20,}}>
<Button
onPress={console.log('presses')}>
<Text style={styles.item}>{item.key}{timeSlots[index+1] ? ' - ' + timeSlots[index+1] :''}</Text>
</Button>
</SafeAreaView>
}}
/>
</View>
)
}
const styles = StyleSheet.create({
bigBlue: {
color: 'blue',
fontWeight: 'bold',
fontSize: 30,
},
item:{
color:'red',
fontSize:20,
},
cards:{
padding: 10,
}
});
// export default ScreenA;
Error message :
Try this for TouchableOpacity:
import {Image, Text, TouchableOpacity, View} from 'react-native';
...
<TouchableOpacity
style={"row"}
onPress={() => {
onClick(params)
}}>
<Image
source={{
uri: image
}}
style={{
alignSelf: 'center',
aspectRatio: 1,
justifyContent: 'flex-end',
overflow: 'hidden',
alignItems: 'center',
position: 'relative',
}}
/>
<View>
<Text>{name}</Text>
</View>
</TouchableOpacity>
And for Button and title error :
<Button
onPress={() => onClick(props)}
title="Press Me"/>

Fetch data from realtime database firebase in react native?

I already have data in my realtime database. I can also fetch it successfully. Now what I need to do is output it in a tinder-like card view. I am currently outputting data using a 'Demo' file which exports a hard-coded array that contains sample information of some users. Using the demo array, the card output is working. But when I fetch data from firebase and store it in an array named items[], nothing is displayed on my output screen. My code is as follows:
Home Screen
import React from 'react';
import { View, ImageBackground } from 'react-native';
import CardStack, { Card } from 'react-native-card-stack-swiper';
import City from '../components/City';
import Filters from '../components/Filters';
import CardItem from '../components/CardItem';
import styles from '../assets/styles';
import Demo from '../assets/demo';;
import {db} from '../config/config';
class Home extends React.Component {
constructor (props) {
super(props);
this.state = ({
items: []
});
}
componentWillMount() {
var items = [];
db.ref('cards').once('value', (snap) => {
snap.forEach((child) => {
let item = child.val();
item.id = child.key;
items.push({
pet_name: child.val().pet_name,
pet_gender: child.val().pet_gender,
pet_age: child.val().pet_age,
pet_breed: child.val().pet_breed,
photoUrl: child.val().photoUrl,
});
});
//console.log(items)
this.setState({ items: items });
console.log(items);
});
}
render() {
return (
<ImageBackground
source={require('../assets/images/bg.png')}
style={styles.bg}
>
<View style={styles.containerHome}>
<View style={styles.top}>
<City />
<Filters />
</View>
<CardStack
loop={true}
verticalSwipe={false}
renderNoMoreCards={() => null}
ref={swiper => {
this.swiper = swiper
}}
>
{Demo.map((item, index) => (
<Card key={index}>
<CardItem
image={item.image}
name={item.name}
description={item.description}
actions
onPressLeft={() => this.swiper.swipeLeft()}
onPressRight={() => this.swiper.swipeRight()}
/>
</Card>
))}
</CardStack>
</View>
</ImageBackground>
)
}
}
export default Home;
Demo Data
module.exports = [
{
id: 3,
name: 'whatever',
status: 'Online',
match: '78',
description:
'Full-time Traveller. Globe Trotter. Occasional Photographer. Part time Singer/Dancer.',
message:
'I will go back to Gotham and I will fight men Iike this but I will not become an executioner.',
image: require('./images/01.jpg')
},
{
id: 2,
name: 'Clementine Bauch',
match: '93',
description:
'Full-time Traveller. Globe Trotter. Occasional Photographer. Part time Singer/Dancer.',
status: 'Offline',
message: "Someone like you. Someone who'll rattle the cages.",
image: require('./images/02.jpg')
}
];
fetching data:
import {db} from '../config/config';
var items = [];
db.ref('cards').once('value', (snap) => {
snap.forEach((child) => {
let item = child.val();
item.id = child.key;
items.push({
pet_name: child.val().pet_name,
pet_gender: child.val().pet_gender,
pet_age: child.val().pet_age,
pet_breed: child.val().pet_breed,
photoUrl: child.val().photoUrl,
});
});
//console.log(items)
for (var i in items){
console.log(items[i])
}
});
export var items;
I want to use my items[] array instead of the Demo hard-coded array. How do I do this?
The output of items[] array:
Array [
Object {
"pet_age": "11",
"pet_breed": "golden retriever",
"pet_gender": "male",
"pet_name": "dawn",
"photoUrl": "picture",
},
Object {
"pet_age": "7",
"pet_breed": "German",
"pet_gender": "Male",
"pet_name": "Rambo",
"photoUrl": "https://firebasestorage.googleapis.com/v0/b/woofmatix-50f11.appspot.com/o/pFkdnwKltNVAhC6IQMeSapN0dOp2?alt=media&token=36087dae-f50d-4f1d-9bf6-572fdaac8481",
},
]
CardItem component:
import React from 'react';
import styles from '../assets/styles';
import { Text, View, Image, Dimensions, TouchableOpacity } from 'react-native';
import Icon from './Icon';
const CardItem = ({
actions,
description,
image,
matches,
name,
pet_name,
pet_gender,
pet_age,
onPressLeft,
onPressRight,
status,
variant
}) => {
// Custom styling
const fullWidth = Dimensions.get('window').width;
const imageStyle = [
{
borderRadius: 8,
width: variant ? fullWidth / 2 - 30 : fullWidth - 80,
height: variant ? 170 : 350,
margin: variant ? 0 : 20
}
];
const nameStyle = [
{
paddingTop: variant ? 10 : 15,
paddingBottom: variant ? 5 : 7,
color: '#363636',
fontSize: variant ? 15 : 30
}
];
return (
<View style={styles.containerCardItem}>
{/* IMAGE */}
<Image source={image} style={imageStyle} />
{/* MATCHES */}
{matches && (
<View style={styles.matchesCardItem}>
<Text style={styles.matchesTextCardItem}>
<Icon name="heart" /> {matches}% Match!
</Text>
</View>
)}
{/* NAME */}
<Text style={nameStyle}>{name}</Text>
{/* DESCRIPTION */}
{description && (
<Text style={styles.descriptionCardItem}>{description}</Text>
)}
{/* STATUS */}
{status && (
<View style={styles.status}>
<View style={status === 'Online' ? styles.online : styles.offline} />
<Text style={styles.statusText}>{pet_age}</Text>
</View>
)}
{/* ACTIONS */}
{actions && (
<View style={styles.actionsCardItem}>
<View style={styles.buttonContainer}>
<TouchableOpacity style={[styles.button, styles.red]} onPress={() => {
this.swiper.swipeLeft();
}}>
<Image source={require('../assets/red.png')} resizeMode={'contain'} style={{ height: 62, width: 62 }} />
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.orange]} onPress={() => {
this.swiper.goBackFromLeft();
}}>
<Image source={require('../assets/back.png')} resizeMode={'contain'} style={{ height: 32, width: 32, borderRadius: 5 }} />
</TouchableOpacity>
<TouchableOpacity style={[styles.button, styles.green]} onPress={() => {
this.swiper.swipeRight();
}}>
<Image source={require('../assets/green.png')} resizeMode={'contain'} style={{ height: 62, width: 62 }} />
</TouchableOpacity>
</View>
</View>
)}
</View>
);
};
export default CardItem;

React native how to animate view opening up->down

Consider the following component where the user selects an option from a list:
import React, { Component } from "react";
import PropTypes from "prop-types";
import {
View,
Text,
StyleSheet,
Platform,
FlatList,
TouchableNativeFeedback,
TouchableOpacity,
PLatform
} from "react-native";
import Icon from "react-native-vector-icons/Ionicons";
import { colors, metrics } from "../../themes";
const Touchable =
Platform.OS === "android" ? TouchableNativeFeedback : TouchableOpacity;
class MenuSelector extends Component<{
onSelect: () => any,
config: object,
selected: string
}> {
state = {
listOpened: false
};
handlePress = id => {
this.props.onPress(id);
};
handleSelect = id => {
if (this.props.onSelect) this.props.onSelect(id);
this.setState({
listOpened: false
});
};
handleMenu = () => {
this.setState({
listOpened: !this.state.listOpened
});
};
render = () => {
let title = "";
if (this.props.config) {
title = this.props.config[0].title;
if (this.props.selected) {
let found = this.props.config.find(item => {
return item.id === this.props.selected;
});
if (found) title = found.title;
}
}
let top = (
<View style={styles.header}>
<Text style={styles.text}>{title}</Text>
<Touchable>
<Text style={styles.text}>
<Icon
name={"ios-menu"}
size={20}
onPress={this.handleMenu}
/>
</Text>
</Touchable>
</View>
);
let list = null;
if (this.state.listOpened === true) {
list = (
<FlatList
data={this.props.config}
renderItem={({ item }) => (
<Touchable onPress={this.handleSelect}>
<Text style={[styles.text, styles.listItem]}>{item.title}</Text>
</Touchable>
)}
/>
);
}
return (
<View style={styles.container}>
{top}
{list}
</View>
);
};
}
export default MenuSelector;
const styles = StyleSheet.create({
container: {
flex: -1,
flexDirection: "column"
},
header: {
flex: -1,
flexDirection: "row",
justifyContent: "space-between",
padding: 10,
backgroundColor: "blue"
},
text: {
fontSize: 16,
color: "white",
textAlign: "center",
fontWeight: "bold"
},
listItem: {
padding: 10,
backgroundColor: "blue"
}
});
The component is used in the following context:
let config = [
{
title="Option 1",
id="option1"
},
{
title="Option 2",
id="option2"
},
{
title="Option 3",
id="option3"
},
{
title="Option 4",
id="option4"
},
];
return (
<View style={styles.container}>
<MenuSelector
config={config.options}
selected={this.state.mode}
onPress={this.handleButtonListPress}
/>
<FlatList
data={this.props.data}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (
<Text style={styles.text}>{item.name}</Text>
)}
/>
</View>
);
As it is, the <MenuSelector> component appears "at once" on screen. I need to add an sliding effect to <MenuSelector>, "pushing down" the data FlatList when appearing on screen...
On closing, same behaviour, but animating from down to up.
How can I add such animation behaviour to my MenuSelector component ?

Resources