MobX

MobX

  • API 참고서
  • 영어 문서
  • 후원자
  • GitHub

›Tips & Tricks

Introduction

  • MobX에 대하여
  • MobX 문서에 대하여
  • 설치 방법
  • MobX의 요지

MobX core

  • Observable state
  • Actions
  • Computeds
  • Reactions {🚀}

MobX and React

  • React 통합
  • React 최적화 {🚀}

Tips & Tricks

  • 데이터 스토어 정의
  • 반응성 이해하기
  • 서브클래싱
  • 반응 분석하기 {🚀}
  • 인수가 필요한 computed {🚀}
  • MobX-utils {🚀}
  • Custom observables {🚀}
  • Lazy observables {🚀}
  • Collection utilities {🚀}
  • Intercept & Observe {🚀}

Fine-tuning

  • 환경설정 {🚀}
  • 데코레이터 사용하기 {🚀}
  • MobX 4 또는 5에서 마이그레이션 하기 {🚀}
Edit

Lazy observables 만들기 {🚀}

사용법:

  • onBecomeObserved(observable, property?, listener: () => void): (() => void)
  • onBecomeUnobserved(observable, property?, listener: () => void): (() => void)

onBecomeObserved와 onBecomeUnobserved 함수는 기존의 관찰 대상들(observables)에 lazy한 동작이나 부작용(side effects)을 추가하는 데 사용할 수 있습니다. 이 함수들은 MobX의 관찰 시스템에 연결되어 있고 관찰 대상들(observables)이 관찰되기 시작 하거나 중단 될 때 알림을 받습니다. 두 함수는 리스너(listener)를 분리하는 처리(disposer) 함수를 리턴합니다.

아래의 예시에서는 관찰된 값(observed value)이 실제로 사용 중일 때만 네트워크 fetch를 수행하는 데 두 함수를 사용합니다.

export class City {
    location
    temperature
    interval

    constructor(location) {
        makeAutoObservable(this, {
            resume: false,
            suspend: false
        })
        this.location = location
        // temperature가 실제로 사용 되는 경우에만 데이터 fetching을 시작합니다!
        onBecomeObserved(this, "temperature", this.resume)
        onBecomeUnobserved(this, "temperature", this.suspend)
    }

    resume = () => {
        log(`Resuming ${this.location}`)
        this.interval = setInterval(() => this.fetchTemperature(), 5000)
    }

    suspend = () => {
        log(`Suspending ${this.location}`)
        this.temperature = undefined
        clearInterval(this.interval)
    }

    fetchTemperature = flow(function* () {
        // 데이터 fetching 로직...
    })
}
← Custom observables {🚀}Collection utilities {🚀} →
MobX
Docs
About MobXThe gist of MobX
Community
GitHub discussions (NEW)Stack Overflow
More
Star