By Sheldon L | Published at 2020-03-11 | Updated at 2020-03-11 |
AndroidStudio 1 - Installation and Start an Android Emulator - For Android
Xcode - For iOS
npx react-native init ShoppingList
# some packages need later
npm i react-native-vector-icons
react-native link react-native-vector-icons
npm i uuidv4
# start android emulator
sudo chmod 467 /dev/kvm
studio.sh android/
react-native start # click green button run device
App.js
import React, {useState} from 'react';
import {View, StyleSheet, FlatList, Alert} from 'react-native';
import {uuid} from 'uuidv4';
import Header from './components/Header';
import ListItem from './components/ListItem';
import AddItem from './components/AddItem';
const App = () => {
const [items, setItems] = useState([
// {id: uuid(), text: 'Milk'},
// {id: uuid(), text: 'Apple'},
]);
const deleteItem = id => {
setItems(prevItems => {
return prevItems.filter(item => item.id !== id);
});
};
const addItem = text => {
if (!text) {
Alert.alert('Error', 'Pleas enter an item...');
} else {
setItems(prevItems => {
return [{id: uuid(), text}, ...prevItems];
});
}
};
return (
<View style={styles.container}>
<Header />
<AddItem addItem={addItem} />
<FlatList
data={items}
renderItem={({item}) => (
<ListItem item={item} deleteItem={deleteItem} />
)}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 60,
},
});
export default App;
components/Header.js
import React from 'react';
import {View, Text, StyleSheet} from 'react-native';
const Header = ({title}) => {
return (
<View style={styles.header}>
<Text style={styles.text}>{title}</Text>
</View>
);
};
Header.defaultProps = {
title: 'Shopping List',
};
const styles = StyleSheet.create({
header: {
height: 60,
padding: 15,
backgroundColor: 'darkslateblue',
},
text: {
color: '#fff',
fontSize: 24,
textAlign: 'center',
},
});
export default Header;
components/ListItem.js
import React from 'react';
import {View, Text, StyleSheet, TouchableOpacity} from 'react-native';
import Icon from 'react-native-vector-icons/dist/FontAwesome';
const ListItem = ({item, deleteItem}) => {
return (
<TouchableOpacity style={styles.listItem}>
<View style={styles.listItemView}>
<Text style={styles.listItemText}>{item.text}</Text>
<Icon
name="remove"
size={20}
color="firebrick"
onPress={() => deleteItem(item.id)}
/>
</View>
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
listItem: {
padding: 15,
backgroundColor: '#f8f8f8',
borderBottomWidth: 2,
borderColor: '#ddd',
},
listItemView: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
listItemText: {
fontSize: 18,
},
});
export default ListItem;
components/AddList.js
import React, {useState} from 'react';
import {
View,
Text,
StyleSheet,
TextInput,
TouchableOpacity,
} from 'react-native';
import Icon from 'react-native-vector-icons/dist/FontAwesome';
const AddItem = ({title, addItem}) => {
const [text, setText] = useState('');
const onChange = textValue => setText(textValue);
return (
<View>
<TextInput
placeholder="Add Item..."
style={styles.input}
onChangeText={onChange}
/>
<TouchableOpacity style={styles.btn} onPress={() => addItem(text)}>
<Text style={styles.btnText}>
<Icon name="plus" size={20} />
Add Item
</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
input: {
height: 60,
padding: 8,
margin: 5,
},
btn: {
backgroundColor: '#c2bad8',
padding: 9,
margin: 5,
},
btnText: {
color: 'darkslateblue',
fontSize: 20,
textAlign: 'center',
},
});
export default AddItem;