studybook
  • Introduction
  • 실무 하며 깨닫는 부분 정리
    • 옵션에 대해서
    • 코드 작성의 순서
    • 자바 프로그램에 문제가 생겼다면
    • 장애 대처법
  • Logstash, Beats 정리
  • Zookeeper 정리
  • Message Queue 정리
    • RabbitMQ 삽질
  • Java 관련 정리
    • Java Primitive Wrapper class
    • Java NIO
    • Java8 Double colon operator
    • Effective Java
      • 4장
      • 5장
      • 6장 - Enum, Annotation
      • 7장 - Method
      • 8장 - 프로그래밍 일반
      • 9장 - Exception
    • Java8 Lambda expression
    • JDBC
    • Linux에서 WatchService 이상동작
  • Spring 관련 정리
    • Spring Bean init, destroy 순서
    • Spring Async Controller
    • Spring Executable jar 웹 개발 및 배포
    • Spring Boot Font 배포 에러
    • Spring AOP
      • Spring AOP로 모든 Request 로그 남기기
    • Spring Cache
    • Spring Cloud
      • Consul로 spring 설정 관리하기
    • Spring Test
      • Spring Test DirtiesContext
      • Spring Test MockBean, SpyBean
      • Spring Test Dynamic @Scheduled
    • Spring JDBC
    • Spring Validation
    • Spring Transaction Management
      • Spring with JTA 삽질
    • Spring에서 효율적으로 Static resource 관리하기
    • Zuul을 사용해서 Spring Reverse proxy 만들기
    • Spring Security
    • 스프링 어노테이션이 안 먹힐 때 의심해볼만한 것
    • Spring Data
    • Spring Webflux
      • Tobi 강연
  • 코드 리팩토링
    • 한번에 하나씩
  • 지속적 통합 (CI)
    • Jenkins pipeline 삽질기
  • Log Aggregator 정리
    • Flume 테스트
    • Fluentd 테스트
  • Web Socket 정리
  • Akka
    • Actor 모델
    • Supervision
  • IE 8 대응 정리
  • 함수형 프로그래밍
    • 모나드
  • Netty
    • Netty 기본 예제
    • Netty 주요 특징
    • Netty 부트스트랩
    • Netty 채널 파이프라인, 코덱
    • Netty 이벤트 모델
    • Netty 바이트 버퍼
  • 스칼라 관련 정리
    • Maven으로 컴파일하기
    • Scala def 괄호 여부의 차이
    • 스칼라 function, method 차이점
    • ScalaTest와 Spring 연동하기
    • Programming in Scala
  • J2S 컨퍼런스
  • Android
    • 테스트
    • NDK
  • DDOS
  • HTTP
  • HttpClient
  • Container
    • Image 개요
    • cri-o
    • kata containers
    • Open Container Initiative Image
    • Buildkit
  • Github pages
  • Static Website
  • Webhook
  • Service Discovery Tools
    • Etcd
    • Eureka
    • Consul
      • ACL
    • 비교
  • React
    • JSX
    • React Element
    • Components, Props
    • State, Lifecycle
    • Handling Event
    • Flux
  • Vagrant
    • SSH 접속
  • Linux
    • Systemd
    • Alternatives
  • Messaging protocols
    • XMPP
    • AMQP
  • Windows
    • Windows10 내장 우분투에 ssh 클라이언트로 접속하기
    • Windows10 Hyper-V와 Virtual Box가 충돌을 일으켰을 때
    • Hyper-V 기반 docker에서 Shared Drives 설정 실패할 때
    • 윈도우 개발환경 설정
    • Docker desktop 없이 docker 환경 세팅하기
    • UWP 앱을 항상 관리자권한으로 실행하는 바로가기 만들기
  • Spring camp 2017
    • Project Reactive
    • 이벤트 소싱
    • CQRS
  • Spring webflux
  • 리액티브 프로그래밍
  • Linux Settings
    • 홈서버 백업 및 복구기
    • 홈서버 트러블슈팅
  • Kubernetes
    • k3s 설치 및 삽질
    • pod resources
    • Argo workflow
    • 트러블 슈팅
      • Kubernetes namespace의 phase가 Terminating에서 멈춰있을 때
    • 쿠버네티스 마스터
    • Knative
    • Knative Pipeline
    • Aggrerated API server
    • Accessing the API
      • Authenticating
  • Sonarqube
  • HTTP/2
  • Go
    • Go Module
    • Go dependency injection
    • Go Error handling
    • Go in Action
      • 3장 패키지
      • 4장 배열, 슬라이스, 맵
      • 5장 GO의 타입 시스템
      • 6장 동시성
      • 7장 동시성 패턴
      • 8장 표준 라이브러리
      • 9장 테스트와 벤치마킹
    • Go Channel 사용법
  • Cloud Native
Powered by GitBook
On this page
  • State
  • 사용법
  • 탑 다운 데이터 플로우
  • Lifecycle
  • 종류
  1. React

State, Lifecycle

State

앞서 React Element는 immutable 개체라고 했다. 그러나 component는 stateless 개체가 아니다. component는 어떤 자신의 state를 가질 수 있다. 물론 앞서 설명에는 모든 react component를 pure function처럼 취급하라고 했는데 바로 다음 페이지에서 이렇게 말을 뒤집으니 좀 난감하긴 하다. 뭐, 정말로 stateless한 pure function으로 할 때 여러모로 불편한 점이 생기니 만든 대안이긴 할 것이다.

사용법

우선, 만약에 component를 function으로 작성했다면 es6 class 형태로 바꾸는게 여러모로 유리하다.

import React from 'react';

// 1. use prop
class MyComponent extends React.Component {
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

// 2. use state
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {name: props.name}
  }

  render() {
    return (
      <h1>Hello, {this.state.name}</h1>
    );
  }
}

2번 예제처럼 this.state 에 객체로 저장해두고 자유롭게 사용할 수 있다. 그런데 여기서 궁금해지는게 있다. 그냥 class field로 두고 사용해도 될 것 같은데 왜 하필 꼭 저렇게 써야되나? 이유는 다음과 같다. 그냥 class field로 어떤 state를 저장하고 변경이 발생했을 땐 실제 DOM까지 그 변경이 전파되지 않기 때문이다. 그래서 this.state에다가 저장하고 이 state를 변경할 때도 특정 메소드를 사용해서 변경해야만 한다.

유의사항

  1. state를 직접 수정하지 않아야 함

    1. 이유는 위에서 말한 것 처럼, DOM까지 state의 변경을 전파하기 위해서. 대신에 setState()를 사용

    2. this.state.name = "other'; //false  
      this.setState\({name: 'other'}\); // true
      
      constructor\(props\) {  
        super\(props\);  
        this.state = {name: 'other'}; // 생성자에서는 허용됨  
      }
  2. state의 변경은 비동기적으로 적용됨

    1. react에서 성능을 향상시키기위해 여러 setState()를 몰아서 한번에 적용하기 때문에 발생. 따라서 이전 state에 의존해서 작동하는 코드는 고장날 수 있음. 이럴 땐 setState()의 argument에 callback function을 넘겨서 처리 가능

    2. // 이렇게 여러번 호출하면 호출한 횟수만큼 this.state.counter의 값이 증가해있길 바라지만 
      // 순서가 보장되지 않기 때문에 원하는 값이 나오지 않을 수 있음
      this.setState({
        counter: this.state.counter + this.props.increment
      });
      
      // 고로 이렇게 function을 넣으면 원하는 대로 동작
      this.setState((prevState, props) => ({
        counter: prevState.counter + props.increment
      }));
  3. state의 변경은 엄밀히 따져서 set이 아니라 merge에 가까움

    1. state 내부에 여러 key가 존재할 때, setState()에서 하나씩 수정해도 문제 없음

    2. import React from 'react';
      
      class Point extends React.Component {
        constructor(props) {
          super(props);
          this.state = {
            x: 0,
            y: 0
          }
        }
      
        componentDidMount() {
          calculateX().then(newX => {
            this.setState({x: newX}); // 기존의 y값은 놔두고 x값만 바꿈
          });
      
          calculateY().then(newY => {
            this.setState({y: newY}); // 기존의 x값은 놔두고 y값만 바꿈
          });
        }
      }

탑 다운 데이터 플로우

component의 state는 local, 내지는 encapsulated된 데이터라고 볼 수 있다. 즉, 다른 component에서 접근할 수 없다는 뜻이다. 이 사실은 parent - child 관계에 놓인 component들 끼리도 마찬가지다. 그러나 parent -> child 간의 데이터 전달은 가능하며, 이 경우 parent에서 어떤 데이터를 넘겨주게되면 child에서는 그걸 props로 간주하여 사용한다. 이 때, child는 parent가 전달하는 데이터가 state이든, props든, 상수이든 전혀 신경쓰지 않는다.

import React from 'react';

class Parent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {data: props.obj.data};
  }

  render() {
    // 모두 문제 없음
    return (
      <Child data={this.props.obj.data} />
      <Child data={this.state.data} />
      <Child data='my Data' />
    )
  }
}

class Child extends React.Component {
  render() {
    return (
      <h1>{this.props.data}</h1>
    );
  }
}

Lifecycle

component들은 모두 각자의 라이프 사이클을 가지고 있으며, 이 각각의 라이프 사이클에 대해 원하는 동작을 써 넣을 수 있다. 보통, 메소드 이름에 will이 붙으면 특정 동작을 하기 전에 수행하고, did가 붙으면 동작을 완료한 다음 수행한다.

종류

Mounting

component가 생성되고 DOM에 끼워넣어질 때

  1. constructor()

  2. componentWillMount()

  3. render()

  4. componentDidMount()

Updating

props나 state의 변경에 의해서 component가 다시 렌더링 될 때

  1. componentWillReceiveProps()

  2. shouldComponentUpdate()

  3. componentWillUpdate()

  4. render()

  5. componentDidUpdate()

Unmounting

component가 DOM에서 제거될 때

  1. componentWillUnmount()

PreviousComponents, PropsNextHandling Event

Last updated 7 years ago