-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathApp.js
56 lines (48 loc) · 1.7 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React from 'react';
import { StyleSheet, Text, View, ScrollView, Button, FlatList } from 'react-native';
import {contacts, newRandomKey} from './contacts'
import Constants from 'expo-constants'
import ContactList from './ContactList'
import AddContact from './AddContact'
export default class ContactApp extends React.Component {
// this is a shorthand way of writing a constructor when we only have state
// there is a standard constructor here:
// https://github.com/rrobbes/EngineeringOfMobileSystems/blob/master/lec4-ReactNative/todo-rn.js
state = {
contacts: contacts,
showContacts: true,
}
// we define the callbacks using the arrow syntax here
// before, we were creating them in the onPress props:
// https://github.com/rrobbes/EngineeringOfMobileSystems/blob/master/lec4-ReactNative/todo-rn.js
// the advantage of this way is that we create the arrow function once
// the way we did before, a new arrow function was created each time
// an object was rendered
toggleContacts = () => {
this.setState(prev => ({showContacts: !prev.showContacts}))
}
addContact = newContact => {
const contactWithKey = {...newContact, key: newRandomKey()}
this.setState(prev => ({contacts: [...prev.contacts, contactWithKey], showContacts: true}))
}
render() {
return (
<View style={styles.container}>
<Button
title="Enter New Contact"
onPress={this.toggleContacts}/>
{this.state.showContacts?
<ContactList contacts={this.state.contacts}/>:
<AddContact onSubmit={this.addContact}/>}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
paddingTop: Constants.statusBarHeight,
flex: 1,
backgroundColor: '#fff',
justifyContent: 'center',
},
});