일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 다이나믹프로그래밍
- 자바 #java
- react #리액트 #동빈나 #나동빈 #유튜브강의
- 알고리즘
- 프로그래머스
- java #자바
- 파이썬 #백준 #알고리즘 #코딩테스트
- 파이썬 #알고리즘 #코딩테스트 #프로그래머스
- 프로그래머스 #파이썬 #알고리즘 #코딩테스트
- BFS
- 프로그래머스 #파이썬 #코딩테스트 #알고리즘
- PYTHON
- 파이썬
- DFS
- java #자바 #동빈나
- 백트랙킹
- 백준 #파이썬 #알고리즘 #코딩테스트
- dp
- 투포인터
- Dijkstra
- 다익스트라
- css #생활코딩 #웹
- java #자바 #생활코딩
- 코딩테스트
- 백준
- css #웹 #생활코딩
- 백준 #알고리즘 #파이썬 #코딩테스트
- java #자바 #나동빈
- react #리액트 #동빈나
- 재귀
Archives
- Today
- Total
커리까지
[동빈나] 5강 - React의 State 본문
728x90
SMALL
state
- 내부적으로 변경될 수 있는 데이터를 처리할 때 효율적으로 사용
class Clock extends React.Component {
constructor(props){
super(props);
this.state=
}
render(){
return (
<h3>현재 시각은 [{this.props.date.toLocaleTimeString()}] 입니다.</h3>
);
}
}
function tick() {
ReactDOM.render(<Clock date={new Date()}/>, document.getElementById('root'));
}
setInterval(tick, 1000)
this.props.
: class를 사용하면 안에 props가 있어서 this를 붙여줘야 한다.super(props)
: constructor를 사용해서 인자를 준다면 super를 해서 props를 초기화해준다.
class Clock extends React.Component {
constructor(props){
super(props);
this.state= {
date : new Date()
};
}
componentDidMount(){
}
render(){
return (
<h3>현재 시각은 [{this.state.date.toLocaleTimeString()}] 입니다.</h3>
);
}
}
ReactDOM.render(<Clock/>,document.getElementById('root'));
- 더이상 다른 함수는 필요없다. class 안에서 다 할거다.
- state를 줬지만 새로고침하는 명령이 없어서 정적으로 되어있다.
class Clock extends React.Component {
constructor(props){
super(props);
this.state= {
date : new Date()
};
}
tick() {
this.setState({
date: new Date()
})
}
componentDidMount(){
this.timerID = setInterval(() => this.tick(),1000 );
}
componentWillUnmount(){
clearInterval(this.timerID);
}
render(){
return (
<h3>현재 시각은 [{this.state.date.toLocaleTimeString()}] 입니다.</h3>
);
}
}
ReactDOM.render(<Clock/>,document.getElementById('root'));
- state를 바꾸려면 setState를 줘서 값을 다시 준다.
class Clock extends React.Component {
constructor(props){
super(props);
this.state= {
date : new Date()
};
}
goBack () {
let nextDate = this.state.date;
nextDate.setSeconds(nextDate.getSeconds() -10);
this.setState({
date:nextDate
});
}
render(){
return (
<div>
<h3>현재 시각은 [{this.state.date.toLocaleTimeString()}] 입니다.</h3>
<button onClick={this.goBack.bind(this)}>10초 뒤로가기</button>
</div>
);
}
}
ReactDOM.render(<Clock/>,document.getElementById('root'));
- 버튼을 눌러서 setSstate값을 뒤로 돌릴 수 있다.
728x90
LIST
'react' 카테고리의 다른 글
[react 리액트] Error: Node Sass version 5.0.0 is incompatible with ^4.0.0. (0) | 2021.04.09 |
---|---|
[동빈나] 6강 - React의 LifeCycle과 API 호출 (0) | 2021.04.06 |
[동빈나] 4강 - React의 Component와 Props (0) | 2021.04.01 |
[동빈나] 3강 - React에서 JSX란 (0) | 2021.03.31 |
[동빈나] 2강 - 코드펜(Codepen)을 이용한 React 개발환경 구축하기 (0) | 2021.03.30 |
Comments