https://www.youtube.com/watch?v=Hf4MJH0jDb4
iOS: Latest Xcode & iOS Simulator
Android: Android Studio / SDK / Emulator (AVD)
Install ReactNative, Xcode, Andoird Studio
- https://reactnative.dev/docs/environment-setup
- https://developer.apple.com/xcode/. Open XCode simulator using open -a Simulator
from terminal
- https://developer.android.com/studio
Create a new project using npx react-native init ShoppingList
Note if you encounter an error
Try `/Users/username/Library/Caches/CocoaPods/Pods/Release/Flipper-Glog/0.3.6-1dfd6/missing --help' for more information
configure: WARNING: 'missing' script is too old or missing
Go to XCode
=> Preferences
=> Location
and Select the XCode Location. https://github.com/facebook/react-native/issues/29203
Start the applications
- For iOS: npx react-native run-ios
- For Android: npx react-native run-android
Vector icons
npm i react-native-vector-icons
or using yarn yarn add react-native-vector-icons
npx react-native link react-native-vector-icons
UUID
npm i uuidv4
or using yarn yarn add uuidv4
Two ways to write components using class or functions. https://djoech.medium.com/functional-vs-class-components-in-react-231e3fbd7108
For this tutorial we will be using Functional Components
App.js
import React from 'react';
import { View, Text } from 'react-native';
const App = () => {
return (
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text style={{color: 'darkslateblue', fontSize: 30}}>Hello World</Text>
</View>
)
}
export default App;
https://reactnative.dev/docs/view
App.js
import React from 'react';
import { View, Text, Image, StyleSheet } from 'react-native';
const App = () => {
return (
<View style={styles.container}>
<Text style={styles.text}>Hello World</Text>
<Image source={{uri: 'https://randomuser.me/api/portraits/men/1.jpg'}} style={styles.img} />
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
color: 'darkslateblue',
fontSize: 30,
},
img: {
width: 100,
height: 100,
borderRadius: 100 / 2
},
});
export default App;
App.js
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Header from './components/Header';
const App = () => {
return (
<View style={styles.container}>
<Header />
</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 = () => {
return (
<View style={styles.header}>
<Text style={styles.text}>Shopping List</Text>
</View>
)
}
const styles = StyleSheet.create({
header: {
height: 60,
padding: 15,
backgroundColor: 'darkslateblue',
},
text: {
color: 'white',
fontSize: 23,
textAlign: 'center',
},
});
export default Header;
App.js
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Header from './components/Header';
const App = () => {
return (
<View style={styles.container}>
<Header title="Shopping List" />
</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 = (props) => {
return (
<View style={styles.header}>
<Text style={styles.text}>{props.title}</Text>
</View>
)
}
const styles = StyleSheet.create({
header: {
height: 60,
padding: 15,
backgroundColor: 'darkslateblue',
},
text: {
color: 'white',
fontSize: 23,
textAlign: 'center',
},
});
Header.defaultProps = {
title: 'Default Title'
}
export default Header;
App.js
import React, {useState} from 'react';
import { View, Text, StyleSheet, FlatList } from 'react-native';
import Header from './components/Header';
import ListItem from './components/ListItem';
const App = () => {
const [items, setItems] = useState([
{id: '1', text: 'Milk'},
{id: '2', text: 'Eggs'},
{id: '3', text: 'Bread'},
{id: '4', text: 'Bagel'},
]);
const deleteItem = (id) => {
setItems(prevItems => {
return prevItems.filter(item => item.id != id);
});
};
return (
<View style={styles.container}>
<Header title="Shopping List" />
<FlatList
data = {items}
renderItem={({item}) => (
<ListItem item={item} deleteItem={deleteItem} />
)}
/>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 60,
},
});
export default App;
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: 1,
borderColor: '#eee',
},
listItemView: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
},
listItemText: {
fontSize: 18,
}
});
export default ListItem;
App.js
import React, {useState} from 'react';
import { View, Text, StyleSheet, FlatList } from 'react-native';
import Header from './components/Header';
import ListItem from './components/ListItem';
import AddItem from './components/AddItem';
const App = () => {
const [items, setItems] = useState([
{id: '1', text: 'Milk'},
{id: '2', text: 'Eggs'},
{id: '3', text: 'Bread'},
{id: '4', text: 'Bagel'},
]);
const deleteItem = (id) => {
setItems(prevItems => {
return prevItems.filter(item => item.id != id);
});
};
const addItem = (text) => {
setItems(prevItems => {
return [{id: '5', text}, ...prevItems];
});
}
return (
<View style={styles.container}>
<Header title="Shopping List" />
<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/AddItem.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,
fontSize: 16,
},
btn: {
backgroundColor: '#c2bad8',
padding: 9,
margin: 5,
},
btnText: {
color: 'darkslateblue',
fontSize: 20,
textAlign: 'center',
},
});
export default AddItem;
import React, {useState} from 'react';
import { View, Text, StyleSheet, FlatList, Alert } from 'react-native';
import Header from './components/Header';
import ListItem from './components/ListItem';
import AddItem from './components/AddItem';
const App = () => {
const [items, setItems] = useState([
{id: '1', text: 'Milk'},
{id: '2', text: 'Eggs'},
{id: '3', text: 'Bread'},
{id: '4', text: 'Bagel'},
]);
const deleteItem = (id) => {
setItems(prevItems => {
return prevItems.filter(item => item.id != id);
});
};
const addItem = (text) => {
if(!text) {
Alert.alert('Error', 'Please enter an item', { text: 'Ok'})
} else {
setItems(prevItems => {
return [{id: '5', text}, ...prevItems];
});
}
}
return (
<View style={styles.container}>
<Header title="Shopping List" />
<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;