MobX - woowa-techcamp-2021/store-4 GitHub Wiki

MobX

๊ณต์‹๋ฌธ์„œ, ์ฐธ๊ณ 

MobX ํ•œ๊ธ€ ๋ฌธ์„œ https://ko.mobx.js.org/README.html

React์—์„œ MobX ๊ฒฝํ—˜๊ธฐ https://techblog.woowahan.com/2599/

์†Œ๊ฐœ

๊ฐ„๋‹จํ•˜๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ๊ด€๋ฆฌ

์ฒ ํ•™

  1. Straightforward (์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๋‹ค)

    ํ• ๋‹น๋ฌธ์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ state ๋ฅผ ๋ณ€๊ฒฝ

  2. Effortless optimal rendering (๋žœ๋”๋ง ์ตœ์ ํ™”ํ•˜๊ธฐ ์‰ฝ๋‹ค)

    ์ƒํƒœ์™€ ์ƒํƒœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ด€๊ณ„๋ฅผ ์˜์กด ํŠธ๋ฆฌ (dependency tree)๋กœ ๋งŒ๋“ค๊ณ  ์ƒํƒœ์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋งŒ ์—ฐ์‚ฐ

    • selectors, memoization์„ ์‚ฌ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ์ตœ์ ํ™” ์ž‘์—…์ด ํ•„์š”์—†์Œ
  3. Architectural freedom (๊ตฌ์กฐ์˜ ์ž์œ ) UI ํ”„๋ ˆ์ž„์›Œํฌ ๋ฐ–์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌ

    • ์ฝ”๋“œ ๋ถ„๋ฆฌ๊ฐ€ ์‰ฌ์›€
    • ํ…Œ์ŠคํŠธ๊ฐ€ ์‰ฌ์›€

์˜ˆ์ œ

import React from "react"
import ReactDOM from "react-dom"
import { makeAutoObservable } from "mobx"
import { observer } from "mobx-react"

class Timer {
    secondsPassed = 0

    constructor() {
        makeAutoObservable(this)
    }

    increase() {
        this.secondsPassed += 1
    }

    reset() {
        this.secondsPassed = 0
    }
}

const myTimer = new Timer()

const TimerView = observer(({ timer }) => (
    <button onClick={() => timer.reset()}>Seconds passed: {timer.secondsPassed}</button>
))

ReactDOM.render(<TimerView timer={myTimer} />, document.body)

setInterval(() => {
    myTimer.increase()
}, 1000)

Mobx์˜ ๋ฐ์ดํ„ฐ ํ๋ฆ„

MobX unidirectional flow

mobx-data-flow

์žฅ์ 

์œ„์—์„œ ์–ธ๊ธ‰๋œ ์žฅ์  ์ œ์™ธ

  • ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๊ตฌ์กฐ, ๋กœ์ง๊ณผ View์˜ ๋ถ„๋ฆฌ mobx-spring-layer-table
    • Store: ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ, ์ƒํƒœ ๊ด€๋ฆฌ
    • Model: ๋„๋ฉ”์ธ ๋กœ์ง ์ฒ˜๋ฆฌ
    • Repository: Ajax ์š”์ฒญ ์ฒ˜๋ฆฌ

๋‹จ์ 

  • ๋””๋ฒ„๊น… ํˆด์˜ ๋ถ€์žฌ

  • ์ž์œ ๋กœ์šด ๊ตฌ์กฐ

    • Store๊ฐ€ ์ƒํƒœ(state) ์™€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์„ ํ•จ๊ฒŒ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— Store์˜ ์—ญํ• ์ด ๋ช…ํ™•ํ•ด์•ผํ•จ
    • Store๊ฐ€ ์™ธ๋ถ€ Store๋ฅผ ์ฐธ๊ณ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋ณต์žกํ•œ ๊ตฌ์กฐ๋ฅผ ๋„๊ฒŒ ๋จ
โš ๏ธ **GitHub.com Fallback** โš ๏ธ