-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
106 lines (87 loc) · 2.66 KB
/
app.js
File metadata and controls
106 lines (87 loc) · 2.66 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//import ui lib to be used
import styled from 'styled-components';
// styled-components lib make me write css in js
import React,{Component} from 'react';
//if i put compunent up here with import i don hav to say "class somethinf extends React.Component{}"
import ReactDOM from 'react-dom';
let Input = styled.input `
width:100%;
height:40px;
border:2px solid black;
padding:0% 2%;
`
let Button = styled.button `
border:none;
background-color:red;
color:white;
padding:10px;
`
//define a stateful components
class Todos extends Component{
constructor(){
//initial state
super()
this.state = {
todos:[],
inputValue:'hi'
}
}
//when the user is writing in the input update the var in the key
oninputchange(event){
this.setState({
inputValue: event.target.value
})
}
onKeyUp(event){
let todos = this.state.todos
//we can use the name of the var in another scoop
if (event.key === 'Enter'){
console.log(event.key)
todos.unshift({
text: this.state.inputValue,
status:'unchecked'
})
console.log(todos)
this.setState({
//to asign the value of the new todos thats is on the keyup then asign it to the array
todos:todos,
inputValue:''
//to make the input value empty
})
}
}
render(){
//the displayable things
return(
//in any entry comp it sould be just one parent
//bind makes "this" refers to the class this not to the function this cause this of the function is not this of the class
<div className="container">
<Input type="text" value={this.state.inputValue} onKeyUp={this.onKeyUp.bind(this)} onChange={this.oninputchange.bind(this)}></Input>
{
//this curly phrases is to itirate in the array and map the opjects of it in a loop
this.state.todos.map((item , i)=>{
return <Todo key={i} item={item}/>
})
//every compunent in a loop shuld have a unique id
}
</div>
)
}
}
function Todo(props){
//i dont need the key here so i put it in the todo
return (
<div>
<div className="item" >{props.item.text}</div>
<Button>remove</Button>
</div>
)
}
//define a ststeless component
function App(){
return(
<Todos />
)
}
//display the page content
ReactDOM.render(<App/>,document.getElementById('root'))