Delete multiple items using state in FlatList. React Native - reactjs

I am trying to make a to do list using React Native. How can I delete multiple items in FlatList all the items that has the state of completed: true with a TouchableOpacity(acting like a button). I do not know what methods I should put in function deleteCompleteTodo()
import React, {useState} from 'react';
import { Text, SafeAreaView, StatusBar, FlatList, View, TouchableOpacity,Button, Alert } from 'react-native';
import TodoInput from "./TodoInput";
import TodoItem from "./TodoItem";
const App = () => {
const [todoItems, setTodoItems] = useState([{text: "Buy groceries", completed: true}, {text: "Make blogpost", completed: false}]);
// Add a new item to the state
function addTodoItem(_text) {
setTodoItems([...todoItems, {text:_text, completed: false}]);
}
// Delete an item from state by index
function deleteTodoItem(_index){
let tempArr = [...todoItems];
tempArr.splice(_index, 1);
setTodoItems(tempArr)
}
// Function to set completed to true by index.
function completeTodoItem(_index){
let tempArr = [...todoItems];
if(tempArr[_index].completed){
tempArr[_index].completed = false
}
else{
tempArr[_index].completed = true
}
setTodoItems(tempArr)
}
// Function to delete all completed task
function deleteCompleteTodo(){
let tempArr = [...todoItems];
var i=0;
tempArr.map()
}
// Render
return (
<>
<View style={{flex: 1}}>
<SafeAreaView style={{justifyContent: 'flex-start', flex: 1, backgroundColor: '#dfdfdf', padding: 14, paddingTop: 35}}>
<StatusBar barStyle={"light-content"} backgroundColor={"#212121"}/>
<View style={{flexDirection: 'row', height: 45, justifyContent: 'space-between', marginBottom: 10, borderRadius: 10, backgroundColor: '#d4d1c9'}}>
<View style={{paddingHorizontal: 10, paddingVertical: 5}}>
<Text style={{fontSize: 36, fontWeight: 'bold'}}>Todo</Text>
</View>
{/*Button*/}
<View style={{paddingHorizontal: 10, paddingVertical: 5}}>
{/* <Text style={{fontSize: 36, fontWeight: 'bold'}}>D</Text> */}
<TouchableOpacity
style={{width:50, paddingVertical:8 , backgroundColor: '#ff0000', justifyContent: 'center', alignItems: 'center', borderRadius: 100}}
onPress={() => deleteCompleteTodo()}>
<Text style={{color: '#fafafa', fontWeight: 'bold'}}>Delete</Text>
</TouchableOpacity>
</View>
</View>
<FlatList
data={todoItems}
keyExtractor={(item, index) => index.toString()}
renderItem={({item, index}) => {
return (
<TodoItem
item={item}
deleteFunction={() => deleteTodoItem(index)}
completeFunction={() => completeTodoItem(index)}
/>
)
}}
/>
<TodoInput onPress={addTodoItem} />
</SafeAreaView>
</View>
</>
);
};
export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

You can use filter to delete multy items:
deleteCompleteTodo = () => {
setTodoItems(preState => preState.filter(item => !item.completed))
}

Related

TextInput field retrieving undefined and not showing the value on the list screen

I am trying to add some new items on a list but when i print the list on console log it adds the item but it shows me undefined of name and description. Apparantly there is something wrong with the inputs but I cannot figure out why.
Also on the app itself the it shows that a new item is added but without data.
import React ,{useState}from 'react';
import { KeyboardAvoidingView, StyleSheet,Text,View,TextInput,TouchableOpacity,Keyboard,ScrollView } from 'react-native';
import Task from './components/task';
export default function App(){
const [recipeName,setRecipeName]=useState("");
const [descriptionItem, setDescriptionItem] = useState("");
const [items, setItems] = useState([
{ itemName: "Chicken", description: "chicken test", id: 0 }
]);
const handleAddButtonClick = () => {
const newItem = {
itemName: recipeName, // change
descriptionItem: descriptionItem,
id: items.length
};
console.log(newItem);
const newItems = [...items, newItem];
setItems((state) => {
console.log(state);
console.log(newItems);
return newItems;
});
// setRecipeName("");
// setDescriptionItem("");
// console.log(items.description);
// console.log(items.id); //...
};
return(
<View style={styles.container}>
{/* Added this scroll view to enable scrolling when list gets longer than the page */}
<ScrollView
contentContainerStyle={{
flexGrow: 1
}}
keyboardShouldPersistTaps='handled'
>
{/* Today's Tasks */}
<View style={styles.tasksWrapper}>
<Text style={styles.sectionTitle}>Today's tasks</Text>
<View style={styles.items}>
{/* This is where the tasks will go! */}
{
items.map((item, index) => {
return (
<TouchableOpacity key={index} onPress={() => completeTask(index)}>
<Text>Recipe {item.itemName} Description: {item.description}</Text>
</TouchableOpacity>
)
})
}
</View>
</View>
</ScrollView>
{/* Write a task */}
{/* Uses a keyboard avoiding view which ensures the keyboard does not cover the items on screen */}
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
style={styles.writeTaskWrapper}
>
<View style={{flexDirection: 'column', flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<TextInput style={styles.input} placeholder={'Write a name'} value={recipeName} onChangeText={(text) => setRecipeName(text)} />
<TextInput style={styles.input} placeholder={'Write a date'} value={descriptionItem} onChange={(text) => setDescriptionItem(text)} />
</View>
<TouchableOpacity onPress={() => handleAddButtonClick()}>
<View style={styles.addWrapper}>
<Text style={styles.addText}>+</Text>
</View>
</TouchableOpacity>
</KeyboardAvoidingView>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#E8EAED',
},
tasksWrapper: {
paddingTop: 80,
paddingHorizontal: 20,
},
sectionTitle: {
fontSize: 24,
fontWeight: 'bold'
},
items: {
marginTop: 30,
},
writeTaskWrapper: {
position: 'absolute',
bottom: 60,
width: '100%',
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center'
},
input: {
paddingVertical: 15,
paddingHorizontal: 15,
backgroundColor: '#FFF',
borderRadius: 60,
borderColor: '#C0C0C0',
borderWidth: 1,
width: 250,
},
addWrapper: {
width: 60,
height: 60,
backgroundColor: '#FFF',
borderRadius: 60,
justifyContent: 'center',
alignItems: 'center',
borderColor: '#C0C0C0',
borderWidth: 1,
},
addText: {},
});
When iterating over items in your map() function here:
items.map((item, index) => {
return (
<TouchableOpacity
key={index}
onPress={() => completeTask(index)}
>
<Text>
Recipe {item.itemName} Description: {item.description}
</Text>
</TouchableOpacity>
);
})
You are not using the correct state value. Instead of item.description, it should be item.descriptionItem.
I also advise you to move onChange events to separate methods and set the state inside them, do not use anonymous functions. So, for example, for your description, it would be something like this:
const handleDescription = (e) => {
setDescriptionItem(e.target.value);
};
And in your JSX:
<TextInput
style={styles.input}
placeholder={"Write a date"}
value={descriptionItem}
onChange={handleDescription}
/>
UPDATE:
I recreated your code in sandbox, with minor changes:
commented out the import of Task component (since I don't know what that component does)
disabled onPress event handler as I don't have access to completeTask function
changed the onChangeText for recipeName to onChange
extracted both onChange events to separated methods.
fixed initial state for items; it also had description , instead of descriptionItem
Please, feel free to check it out.

AsyncStorage not storing data and subsequently does not retrieve data

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.
}
}

how to update state according to id?

I have a touchableopacity on each card where I want to setstate of expand to true, but I want to do it according to the id, so that state of only one changes, any idea how to do it using map()?
My code:
import React, {useState, useEffect} from 'react';
import {
SafeAreaView,
Text,
Image,
ScrollView,
TouchableOpacity,
View,
} from 'react-native';
import axios from 'axios';
import {ROOT} from '../../../../ApiUrl';
import Icon from 'react-native-vector-icons/FontAwesome';
export default function VaccinationListScreen(props) {
const [expand, setExpand] = useState(false);
const [data, setData] = useState('');
let id = props.route.params.id;
const getData = () => {
let url = `some url`;
console.log('bbb');
axios
.get(url)
.then(function (res) {
console.log(res.data.content);
setData(res.data.content);
})
.catch(function (err) {
console.log(err);
});
};
useEffect(() => {
getData();
}, []);
return (
<SafeAreaView>
<ScrollView>
<TouchableOpacity style={{padding: 10}} onPress={()=>setExpand(true)}>
{data != undefined &&
data != null &&
data.map((item) => {
return (
<View
style={{
padding: 10,
backgroundColor: '#fff',
elevation: 3,
margin: '2%',
borderRadius: 5,
}}
key={item.id}>
<View style={{alignItems: 'flex-end'}}>
<Text style={{color: 'grey', fontSize: 12}}>
{item.display_date}
</Text>
</View>
<View style={{flexDirection: 'row'}}>
<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>
<View style={{flexDirection: 'row', width: '30%'}}>
{item.vaccine_list.map((i) => {
return (
<View style={{flexDirection: 'row'}}>
<Text
numberOfLines={1}
ellipsizeMode="tail"
style={{fontFamily: 'Roboto', fontSize: 15}}>
{i.name},
</Text>
</View>
);
})}
</View>
</View>
</View>
<View style={{alignItems: 'flex-end', marginTop: '1%'}}>
<View style={{flexDirection: 'row'}}>
<Text
style={{
color: 'red',
fontSize: 14,
fontWeight: 'bold',
}}>
{item.child_vacc_status.text}
</Text>
<Icon
name="chevron-up"
color="red"
size={12}
style={{marginTop: '1%', marginLeft: '1%'}}
/>
</View>
</View>
</View>
);
})}
</TouchableOpacity>
</ScrollView>
</SafeAreaView>
);
}
Any suggestions would be great, do let mw know if anything else is required for better understanding
As i review your code <TouchableOpacity> wraps all of your cards at once not on each card set. If you implement your code that way if it's not impossible it will be difficult for you to reference each cards id and set the state of expand to true according to cards id.
My suggestion is to include <TouchableOpacity> to map() function nest so that it will be easy to reference each cards function.
I reproduce this specific problem and implement a solution in which I was able to set the state of expand to true according to each cards id.
You may click the sandbox link to see a demonstration.
https://codesandbox.io/s/accordingtoid-4px1w
Code in Sandbox:
import React, { useState, useEffect } from "react";
import {
SafeAreaView,
Text,
Image,
TouchableOpacity,
View
} from "react-native";
// import axios from 'axios';
// import {ROOT} from '../../../../ApiUrl';
// import Icon from "react-native-vector-icons/FontAwesome";
export default function VaccinationListScreen(props) {
const [expand, setExpand] = useState({});
const [data, setData] = useState([]);
// let id = props.route.params.id;
// const getData = () => {
// let url = `some url`;
// console.log('bbb');
// axios
// .get(url)
// .then(function (res) {
// console.log(res.data.content);
// setData(res.data.content);
// })
// .catch(function (err) {
// console.log(err);
// });
// };
// useEffect(() => {
// getData();
// }, []);
useEffect(() => {
// In order to simulate and reproduce the problem
// Assume that these are the data that you fetch from an API
const dataContent = [
{
id: 1,
name: "At Birth",
display_date: "02 May - 08 May 16",
vaccine_list: [
{ name: "BCG" },
{ name: "Hepatitis B" },
{ name: "OPV 0" }
],
child_vacc_status: { text: "Missed" }
},
{
id: 2,
name: "At 6 Weeks",
display_date: "02 May - 08 May 16",
vaccine_list: [
{ name: "IPV" },
{ name: "PCV" },
{ name: "Hepatitis b" },
{ name: "DTP" },
{ name: "HiB" },
{ name: "Rotavirus" }
],
child_vacc_status: { text: "Missed" }
}
];
setData(dataContent);
}, []);
function handleOnPress(id) {
setExpand((prev) => {
let toggleId;
if (prev[id]) {
toggleId = { [id]: false };
} else {
toggleId = { [id]: true };
}
return { ...toggleId };
});
}
useEffect(() => {
console.log(expand); // check console to see the value
}, [expand]);
return (
<SafeAreaView>
{data !== undefined &&
data !== null &&
data.map((item) => {
return (
<TouchableOpacity
key={item.id}
style={{
padding: 10
}}
onPress={() => handleOnPress(item.id)}
>
<View
style={{
padding: 10,
backgroundColor: expand[item.id] ? "lightgrey" : "#fff",
elevation: 3,
margin: "2%",
borderRadius: 5
}}
>
<View style={{ alignItems: "flex-end" }}>
<Text style={{ color: "grey", fontSize: 12 }}>
{item.display_date}
</Text>
</View>
<View style={{ flexDirection: "row" }}>
<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>
<View style={{ flexDirection: "row", width: "30%" }}>
{item.vaccine_list.map((item, i) => {
return (
<View key={i} style={{ flexDirection: "row" }}>
<Text
numberOfLines={1}
ellipsizeMode="tail"
style={{ fontFamily: "Roboto", fontSize: 15 }}
>
{item.name},
</Text>
</View>
);
})}
</View>
</View>
</View>
<View style={{ alignItems: "flex-end", marginTop: "1%" }}>
<View style={{ flexDirection: "row" }}>
<Text
style={{
color: "red",
fontSize: 14,
fontWeight: "bold"
}}
>
{item.child_vacc_status.text}
</Text>
</View>
</View>
</View>
</TouchableOpacity>
);
})}
</SafeAreaView>
);
}
I haven't tested the code to work correctly, but you could try something similar. You could create a separate component for the items and set a status for each of them.
export default function VaccinationListScreen(props) {
const [expand, setExpand] = useState(false);
const [data, setData] = useState("");
const VaccinationListItem = (item) => {
const [expand, setExpand] = useState(false);
return (
<TouchableOpacity style={{ padding: 10 }} onPress={() => setExpand(true)}>
<View
style={{
padding: 10,
backgroundColor: "#fff",
elevation: 3,
margin: "2%",
borderRadius: 5,
}}
key={item.id}
>
<View style={{ alignItems: "flex-end" }}>
<Text style={{ color: "grey", fontSize: 12 }}>
{item.display_date}
</Text>
</View>
<View style={{ flexDirection: "row" }}>
<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>
<View style={{ flexDirection: "row", width: "30%" }}>
{item.vaccine_list.map((i) => {
return (
<View style={{ flexDirection: "row" }}>
<Text
numberOfLines={1}
ellipsizeMode="tail"
style={{ fontFamily: "Roboto", fontSize: 15 }}
>
{i.name},
</Text>
</View>
);
})}
</View>
</View>
</View>
<View style={{ alignItems: "flex-end", marginTop: "1%" }}>
<View style={{ flexDirection: "row" }}>
<Text
style={{
color: "red",
fontSize: 14,
fontWeight: "bold",
}}
>
{item.child_vacc_status.text}
</Text>
<Icon
name="chevron-up"
color="red"
size={12}
style={{ marginTop: "1%", marginLeft: "1%" }}
/>
</View>
</View>
</View>
</TouchableOpacity>
);
};
return (
<SafeAreaView>
<ScrollView>
{data != undefined &&
data != null &&
data.map((item) => {
VaccinationListItem(item);
})}
</ScrollView>
</SafeAreaView>
);
}
Generally if you want to toggle any single element you should store its id in expand (instead of a boolean), and simply check when rendering the array if any specific element's id matches, i.e. element.id === expand. When any new element is touched, pop its id in there, if the id is already there, set to null to collapse.
export default function VaccinationListScreen(props) {
const [expandId, setExpandId] = useState(null); // <-- stores null or id, initially null
...
// Create curried handler to set/toggle expand id
const expandHandler = (id) => () =>
setExpandId((oldId) => (oldId === id ? null : id));
return (
<SafeAreaView>
<ScrollView>
{data &&
data.map((item) => {
return (
<View
...
key={item.id}
>
<TouchableOpacity
style={{ padding: 10 }}
onPress={expandHandler(item.id)} // <-- attach callback and pass id
>
...
</TouchableOpacity>
{item.id === expandId && ( // <-- check if id match expand id
<ExpandComponent />
)}
</View>
);
})}
</ScrollView>
</SafeAreaView>
);
}

How to check FlatList if its null or not

How can I check the FlatList if it's null or not and if null I will display something like No Available Booking's? I have tried the code below using short hand operator but it's not working.
{bookings == null ?
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={styles.title}>No Availabe Booking's Yet!</Text>
</View>
:
<FlatList
data={bookings}
renderItem={flatListItem}
refreshing={refresh}
onRefresh={refreshSummary}
keyExtractor={item => item._id}
/>
}
Instead of making checks on the data and conditionally rendering the FlatList and the empty list view, you can use the existing prop provided by the FlatList i.e. ListEmptyComponent. You can read more about the FlatList and its other props in the official documentation of the React-Native here.
A typical usage of the ListEmptyComponent could be:
import React, { PureComponent } from 'react';
import { Text, View, StyleSheet, FlatList } from 'react-native';
export default class BookingsList extends PureComponent {
state = {
bookings: [
// {
// _id: 1,
// title: 'I am a booking'
// }
],
refreshing: false
};
keyExtractor = (item) => String(item._id);
refreshSummary = () => {};
renderBookings = ({ item }) => (
<View style={styles.bookingCard}>
<Text style={styles.title}>{item.title}</Text>
</View>
);
renderItemSeparatorComponent = () => <View style={styles.separator} />;
//render the empty list component in case the data array for the FlatList is empty
renderListEmptyComponent = () => (
<View style={styles.emptyListContainer}>
<Text style={styles.noBookingsFound}>
No Availabe Booking's Yet!
</Text>
</View>
);
render() {
const { bookings, refreshing } = this.state;
return (
<FlatList
data={bookings}
refreshing={refreshing}
renderItem={this.renderBookings}
onRefresh={this.refreshSummary}
ListEmptyComponent={this.renderListEmptyComponent} //<==== here
ItemSeparatorComponent={this.renderItemSeparatorComponent}
contentContainerStyle={styles.list}
keyExtractor={this.keyExtractor}
/>
);
}
}
const styles = StyleSheet.create({
bookingCard: {
backgroundColor: 'white',
padding: 10,
marginTop: 2,
borderBottomWidth: 0.5
},
title: {
fontSize: 16,
fontWeight: 'bold'
},
emptyListContainer: {
alignItems: 'center',
justifyContent: 'center',
},
noBookingsFound: {
fontSize: 16,
},
separator: {
height: 15
},
list: {
paddingHorizontal: 15,
paddingBottom: 40
}
});
Would booking not be an Array for a flatlist ?
return (
{bookings !== undefined && bookings.length > 0 ?
<View>
<FlatList
data={bookings}
renderItem={flatListItem}
refreshing={refresh}
onRefresh={refreshSummary}
keyExtractor={item => item._id}
/>
</View>
:
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={styles.title}>No Availabe Booking's Yet!</Text>
</View>
}
);
** Edited as I think I missed your point!
You can return a conditional view in React Native as follows
return (
<View>
{state.someVar == null ?
(<ACOMPONENT />)
:
(<ADIFFCOMPONENT />)
}
</View>
);
Hopefully that's a better response.
u can use listemptycomponent
Rendered when the list is empty. Can be a React Component Class, a render function, or a rendered element.
https://reactnative.dev/docs/flatlist#listemptycomponent
<FlatList
data={bookings}
renderItem={flatListItem}
refreshing={refresh}
onRefresh={refreshSummary}
keyExtractor={item => item._id}
ListEmptyComponent={<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={styles.title}>No Availabe Booking's Yet!</Text>
</View>}
/>
{ bookings && Array.isArray(bookings) ? (
<FlatList
data={bookings}
renderItem={flatListItem}
refreshing={refresh}
onRefresh={refreshSummary}
keyExtractor={item => item._id}
/>
) : (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={styles.title}>No Availabe Booking's Yet!</Text>
</View>
)
}

how to add keys in this project

im using react o nexpo xde and when i run the project i get a warning because my list doesnt hae keys, i want to know where and how to assing them, this is my code
import React, { Component } from 'react';
import { StyleSheet, Text, View,AppRegistry,Image,ActivityIndicator, FlatList,Navigator,TouchableHighlight, } from 'react-native';
import { StackNavigator } from 'react-navigation';
class Lista extends Component {
static navigationOptions = {
title: 'Lista',
}
constructor(props) {
super(props);
this.state = {
data:[]
};
}
load = async ()=>{
try{
let resp = await fetch('https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=fd829ddc49214efb935920463668608d')
let json = await resp.json()
this.setState({data:json.articles})
} catch (err) { console.log(err) }
}
componentDidMount(){this.load()}
render() {
return (
<View style={{ flex: 1}}>
<View style={{ flex:1,backgroundColor:'gray'}}>
<FlatList
data={this.state.data}
renderItem={({item}) => (
<TouchableHighlight onPress={() => this.props.navigation.navigate('Details', {item})}>
<View style={{ height:100,margin:15,backgroundColor:'skyblue', padding: 10, flexDirection: 'row'}}>
{item.urlToImage !== null &&
<Image source={{uri:item.urlToImage}} style={{width: 90, height: 80 }}/>
}
<View style={{ flex: 1 }}>
<Text style={{ textAlign: 'center',fontWeight: 'bold', fontSize: 18, color: 'white', flex:1, margin:10}}>{item.title}</Text>
<Text style={{ textAlign: 'right',fontWeight: 'bold', fontSize: 11, color: 'white'}}>{item.publishedAt}</Text>
</View>
</View>
</TouchableHighlight>
)}
/>
</View>
</View>
);
}
}
class DetailsScreen extends React.Component {
static navigationOptions = ({ navigation }) => {
const { item } = navigation.state;
return {
title: item ? item.date : 'Details Screen',
}
};
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Image source={{uri:this.props.navigation.state.params.item.urlToImage}} style={{width: 90, height: 80 }}/>
<Text>{this.props.navigation.state.params.item.title}</Text>
<Text>{this.props.navigation.state.params.item.publishedAt}</Text>
</View>
);
}
}
const RootStack = StackNavigator(
{
Lista: {
screen: Lista,
},
Details: {
screen: DetailsScreen,
},
},
{
initialRouteName: 'Lista',
}
);
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
export default class App extends React.Component {
render() {
return <RootStack />;
}
}
i know it has to be something like, key={i} bu i hae tried in some ways and it doesnt work, im just learning react by myself so im a little confused here
ty so much
In your case you need to set up key to each child of <FlatList /> component. By react native docs recomended to use keyExtractor method defined in your component.
keyExtractor = (item, index) => index
render() {
return (
<View style={{ flex: 1}}>
<View style={{ flex:1,backgroundColor:'gray'}}>
<FlatList
data={this.state.data}
keyExtractor={this.keyExtractor}
renderItem={({item}) => (
<TouchableHighlight onPress={() => this.props.navigation.navigate('Details', {item})}>
<View style={{ height:100,margin:15,backgroundColor:'skyblue', padding: 10, flexDirection: 'row'}}>
{item.urlToImage !== null &&
<Image source={{uri:item.urlToImage}} style={{width: 90, height: 80 }}/>
}
<View style={{ flex: 1 }}>
<Text style= {{ textAlign: 'center',fontWeight: 'bold', fontSize: 18, color: 'white', flex:1, margin:10}}>{item.title}</Text>
<Text style= {{ textAlign: 'right',fontWeight: 'bold', fontSize: 11, color: 'white'}}>{item.publishedAt}</Text>
</View>
</View>
</TouchableHighlight>
)}
/>
</View>
</View>
);
}
I set just index of element as key, but you can set as you wont, but make sure it is unique. But using indexes is bad practice, it is not safe.

Resources