Skip to content

Commit d7f5c6d

Browse files
committed
lesson-11
1 parent f16fefc commit d7f5c6d

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

my-project/App.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import React, { useState } from 'react';
22
import { StyleSheet, Text, View, FlatList } from 'react-native';
33
import Header from './components/header';
44
import TodoItem from './components/todoItem';
5+
import AddTodo from './components/addTodo';
56

67
export default function App() {
78
const [todos, setTodos] = useState([
89
{ text: 'buy coffee', key: '1' },
910
{ text: 'create an app', key: '2' },
10-
{ text: 'play on the switch', key: '3' }
11+
{ text: 'play on the switch', key: '3' },
1112
]);
1213

1314
const pressHandler = (key) => {
@@ -16,11 +17,21 @@ export default function App() {
1617
});
1718
};
1819

20+
const submitHandler = (text) => {
21+
setText('');
22+
setTodos(prevTodos => {
23+
return [
24+
{ text, key: Math.random().toString() },
25+
...prevTodos
26+
];
27+
});
28+
};
29+
1930
return (
2031
<View style={styles.container}>
2132
<Header />
2233
<View style={styles.content}>
23-
{/* add todo form */}
34+
<AddTodo submitHandler={submitHandler} />
2435
<View style={styles.list}>
2536
<FlatList
2637
data={todos}

my-project/components/addTodo.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { useState } from 'react';
2+
import { StyleSheet, View, TextInput, Button } from 'react-native';
3+
4+
export default function AddTodo({ submitHandler }) {
5+
[text, setText] = useState('');
6+
7+
const changeHandler = (val) => {
8+
setText(val);
9+
};
10+
11+
return (
12+
<View>
13+
<TextInput
14+
style={styles.input}
15+
placeholder='new todo...'
16+
onChangeText={changeHandler}
17+
value={text}
18+
/>
19+
<Button color='coral' onPress={() => submitHandler(text)} title='add todo' />
20+
</View>
21+
);
22+
}
23+
24+
const styles = StyleSheet.create({
25+
input: {
26+
marginBottom: 10,
27+
paddingHorizontal: 8,
28+
paddingVertical: 6,
29+
borderBottomWidth: 1,
30+
borderBottomColor: '#ddd',
31+
},
32+
});

my-project/package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)