티스토리 뷰
Object Destructuring ( 객체 구조화 )에 대해 알아봅시다.
다음과 같은 예제 코드가 있습니다.
import React from "react";
class App extends React.Component {
state = {
isLoading: true,
isMounting: false,
};
render() {
const { isLoading } = this.state;
return <div>{isLoading ? "Loading..." : "We are ready"}</div>;
}
}
export default App;
이 코드에서 render() 메서드에 있는 변수 isLoading은 중괄호 안에 선언되어있는데 이건 무슨의미일까요?
render() {
const { isLoading } = this.state;
return <div>{isLoading ? "Loading..." : "We are ready"}</div>;
}
변수가 중괄호안에 있는것과 없는 것으로 차이를 알아봅시다
const { isLoading } = this.state;
const isLoading = this.state;
1) const { isLoading } = this.state;
: this.state에 있는 isLoading의 값을 찾아서 변수에 넣어준다
2) const isLoading = this.state;
: this.state의 값 전체를 isLoading에 할당한다
state의 값.
state = {
isLoading: true,
isMounting: false,
};
결과적으로 state에 정의된 값을 고려했을 때
1)번의 { isLoading }은 true값을 갖고
2)번의 isLoading은 this.state의 값 전체(객체)를 할당받게 됩니다.
'토막지식시리즈 > javascript 토막지식' 카테고리의 다른 글
Typescript 타입 종류 알아보기! (any) (0) | 2021.07.10 |
---|---|
[Typescript] Non-null assertion operator (Non-null 단언 연산자) (0) | 2021.06.28 |
vs code에서 typescript run하기 (0) | 2021.01.10 |
JavaScript는 인터프리트언어? 컴파일언어? (0) | 2020.12.15 |
map의 동작과정 / 사용방법 (javascript) (0) | 2020.12.04 |
댓글
최근에 올라온 글
최근에 달린 댓글
TAG
- 20200415
- 20200421
- likelion
- 백준
- 20200424
- 20200510
- 20200512
- 20200319
- 20201204
- chapter7
- 20200406
- 20200420
- 20200317
- 20200330
- 20200425
- 20200624
- 20200429
- 20200417
- chapter8
- 20200423
- 20200413
- 20200504
- 20200804
- 20200503
- 20200427
- 20200622
- 생활코딩리눅스
- 20200403
- 20200502
- 20200428
- Total
- Today
- Yesterday