1. 예제로 확인하는 MobX

import { inject, observer } from 'mobx-react'

interface AppProps {
	data?: number;
	counter?: number;
}

// 구 버전에서는 아래와 같이 썻다.
// 클래스
@inject("cart")
@observer
export default class App extend React.Component<AppProps>{
	render(){
		return (
			<div className="App">
				<h1>
					외부 데이터: {this.props.data} vs. {this.props.counter}
				</h1>
			</div>
		)
	}
}

export default App;
mobx.configure({ enforceActions: "observed" })

class Store {
    @observable githubProjects = []
    @observable state = "pending"

    fetchProjects = flow(function* () {
        // <- note the star, this a generator function!
        this.githubProjects = []
        this.state = "pending"
        try {
            const projects = yield fetchGithubProjectsSomehow() // yield instead of await
            const filteredProjects = somePreprocessing(projects)
            // the asynchronous blocks will automatically be wrapped in actions and can modify state
            this.state = "done"
            this.githubProjects = filteredProjects
        } catch (error) {
            this.state = "error"
        }
    })
}

2. test

6. Q&A 그리고 참고링크

  1. 싱글톤이란?

    객체를 1개만 생성해서 사용하는 방식을 싱글톤이라고 한다.