Event

== 웹상에서의 모든 행위

Event Handling?

Web에서 인터렉션 ? - EVENT의 핵심

  • State를 만든다
  • Handling 함수를 만든다.
  • 이벤트가 발생하는 HTML 태그에서 onEvent명을 통해 handling 함수를 부른다.
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
import React from 'react';
import './App.css';

// 요구사항 1. 시간과 분이 변화하는 것을 보고 싶다.
// 요구사항 2. 동적으로 보고싶다.
class WorldClock extends React.Component {
constructor(props){
super(props)
this.state = {
hour: this.props.time,
minute : 0,
stop : false
}
// this.setState
// this.state.minute += 1; -> 절대 불가
this.timer = setInterval(()=>{
this.setState((state)=>(
state.minute === 59
?{hour: state.hour + 1, minute :0}
: {minute: state.minute + 1}
))
}, 100)
}
//1-2.handling 함수를 만든다
handlingClick = (event) => {
console.log(event.target)
this.setState({stop: event.target.value})
clearInterval(this.timer);
}
// render 미리 약속되있는 함수
render(){
return(
<div className={"WorldClock"}>
<h2> 도시 : {this.props.city} </h2>
<p> 시간 : {this.state.hour}시 {this.state.minute}분</p>
{/* 1-3. onEvent명을 통해 handling 함수를 부른다. */}
<button value={true} onClick={this.handlingClick}>멈춰!</button>
</div>
)
}
}


class App extends React.Component {
constructor(props){
super(props)
//1-1. state를 만든다
this.cityTimeData= [
['서울', 10],
['북한', 11],
['도쿄', 12],
['베이징', 1],
]
//2-1. state를 만든다
this.state ={
content: ''
}
}
//2-2. handling 함수를 만든다
handlingChange = (event) => {
this.setState({content: event.target.value})
}

render() {
return (
<div className="App">
<h1 className = {'myStyle'}>안녕하세요</h1>
<div className= {'post'}>
첫 게시글입니다.
{/* 2-3.onEvent명을 통해 handling 함수를 부른다. */}
<textarea value={this.state.content} onChange={this.handlingChange}></textarea>
</div>
{this.cityTimeData.map((citytime,index)=>
<WorldClock city={citytime[0]} time ={citytime[1]} key ={index} />
)
}
</div>
);
}
}

//컴포넌트 수출
export default App; // -> index.js

Lifecycle

컴포넌트가 계속 변화하기 때문에 중요

State

  1. Constructor
    State 구조 설정
    컴포넌트가 Mount 하기전에 할 설정
    setState X

  2. ComponentDidMount
    필요한 데이터 요청
    각종 비동기 요청(Subscription)

  3. ComponentDidUpdate
    업데이트 이후 수정할 때
    If() { setState() }

  4. ComponentWillUnmount
    데이터 요청 / 비동기 함수 / 타이머 종료
    setState X

  5. 자식 컴포넌트 + 부모의 Update + 최적화
    ShouldComponetUpdate
    PureComponent

HOOK?참고사이트

React컴포넌트참고사이트

하루를 기록하다