Skip to content
ParkSeungHwan edited this page Nov 25, 2020 · 1 revision

๊ธ€๋กœ๋ฒŒ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ.

Redux์™€ ๊ฐ™์ด ๋ฆฌ์•กํŠธ์— ์ข…์†๋œ ๊ฒƒ์ด ์•„๋‹Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์—์„œ ๋‘๋ฃจ๋‘๋ฃจ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ ํ•˜๋‹ค.

ํ”ํžˆ Redux์™€ ๋งŽ์ด ๋น„๊ต๊ฐ€ ๋˜๋Š”๋ฐ, Redux์— ๋น„ํ•˜์—ฌ ๋Ÿฌ๋‹์ปค๋ธŒ๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๋‚ฎ์œผ๋ฉฐ Observer ํŒจํ„ด์„ ์ดํ•ดํ•˜๊ณ  ์žˆ์œผ๋ฉด ์‰ฝ๊ฒŒ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

MobX์˜ ๊ธฐ๋ณธ ์šฉ์–ด

Observable

์•ฑ์—์„œ ๊ด€์ฐฐ ํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋“ค.

Mobx๋Š” ํ•ด๋‹น ๊ฐ’์„ ๊ด€์ฐฐํ•˜๋ฉฐ, ๋ณ€ํ™”์— ๋”ฐ๋ฅธ notify๋ฅผ ํ•œ๋‹ค.

Computed

state์˜ ๋ณ€ํ™”๊ฐ€ ์ƒ๊ฒผ์„ ๋•Œ, ๊ทธ state์— ๋”ฐ๋ผ ์ถ”๊ฐ€์ ์ธ ์—ฐ์‚ฐ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

ex) ์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ํ•ญ๋ชฉ์ด ์ถ”๊ฐ€ ๋˜์—ˆ์„ ๋•Œ total์„ ๊ณ„์‚ฐํ•จ.

state์˜ ๋ณ€ํ™”๊ฐ€ ์—†์œผ๋ฉด ๊ธฐ์กด ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค. (์„ฑ๋Šฅ ์ตœ์ ํ™”์— ๋งŽ์ด ์‚ฌ์šฉ)

Actions

์ƒํƒœ์— ์–ด๋–ค ๋ณ€ํ™”๋ฅผ ์ผ์œผํ‚ฌ ๊ฒƒ์ธ์ง€ ์ •์˜ ํ•˜๋Š” ๋ถ€๋ถ„.

๋ฆฌ๋•์Šค์™€ ๋‹ฌ๋ฆฌ ๋”ฐ๋กœ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์ง€๋Š” ์•Š์Œ.

Reactions

computed ๋น„์Šทํ•œ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ computed๋Š” ํŠน์ • ๊ฐ’์„ ์—ฐ์‚ฐํ•œ๋‹ค๋ฉด, Reactions๋Š” ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

ex) ๊ฐ’์ด ๋ณ€๊ฒฝ ๋˜์—ˆ์„ ๋•Œ log๋ฅผ ์ฐ์Œ .

์ง์ ‘ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

observable

import {observable} from 'mobx';

const values = observable ({
	num1 : 1, 
	num2 : 2,
})

observable ๋กœ ์ƒ์„ฑ ๋˜์—ˆ๊ธฐ์— Mobx์— ์ด ๊ฐ’์„ ๊ด€์ฐฐ ํ•˜๊ณ  ์žˆ๋‹ค.

๋•Œ๋ฌธ์— ํ•ด๋‹น ๊ฐ’์ด ๋ณ€ํ™”๋ฅผ ๋ฐ”๋กœ ํƒ์ง€ ํ•œ๋‹ค.

reaction

import {observable, reaction} from 'mobx';

const values = observable ({
	num1 : 1, 
	num2 : 2,
})

reaction (
	()=> values.num1, // ๊ฐ์ง€ ํ•˜๋Š” ํƒ€์ผ“
	(value)=>{
		console.log(`a๊ฐ€ ${value}๋กœ ๋ณ€ํ•จ`);
	} // ๊ฐ์ง€ ํ›„ ๋กœ์ง
)

computed

๊ฐ’์ด ๋ณ€๊ฒฝ ๋  ๋•Œ, ๊ทธ ๊ฐ’์— ํ•„์š”ํ•œ ๊ฐ’์„ ๋ฏธ๋ฆฌ ๊ณ„์‚ฐ ํ•ด๋‘๋Š” ์บ์‹ฑ ์ž‘์—…์„ ํ•˜๊ฒŒ ๋œ๋‹ค.

import {observable, computed} from 'mobx';

const values = observable ({
	num1 : 1, 
	num2 : 2,
})

const sum = computed (()=>{ 
	return values.num1 + values.num2
}) 

autorun

reaction์ด๋‚˜ computed ๋Œ€์‹ ์— ์‚ฌ์šฉ ๋  ์ˆ˜ ์žˆ๋‹ค.

autorun์— ์ „๋‹ฌ๋œ ๊ฐ’์„ ์ฃผ์‹œํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€, ๊ทธ ๊ฐ’์ด ๋ณ€๊ฒฝ ๋˜์—ˆ์„ ๋•Œ ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ ํ•œ๋‹ค.

import {observable, autorun, computed} from 'mobx';

const values = observable ({
	num1 : 1, 
	num2 : 2,
})
let sum ; 

autorun(()=>{
	sum = values.num1 + values.num2
})

values.a = 10 
console.log(sum)
// 12 
------- 
const sumComputed = computed(()=>{
	return values.num2 + values.num2
})
autorun(()=> sumComputed.get())

class์— ์ ์šฉํ•˜๊ธฐ

import { decorate, observable, action, computed, autorun } from "mobx";

class Bucket {
  bucket = [];
  get total() {
    return this.bucket.reduce((acc, cur) => acc + cur.price, 0);
  }

  addItem(id, name, price) {
    console.log("add item");
    this.bucket = [...this.bucket, { id, name, price }];
  }
}

decorate(Bucket, {
  bucket: observable,
  total: computed,
  addItem: action
});

const bucket = new Bucket();

bucket.addItem(1, "a", 20); // 'add item'
bucket.addItem(1, "a", 20); // 'add item'
bucket.addItem(1, "a", 20); // 'add item'
bucket.addItem(1, "a", 20); // 'add item'
console.log(bucket.total); // 80 

addItem์„ ํ•  ๋•Œ ๋งˆ๋‹ค ๊ด€๋ จ๋œ ์—ฐ์‚ฐ์ด ๋ฐœ์ƒํ•œ๋‹ค. ๋งŒ์•ฝ, ๋ชจ๋“  ์•ก์…˜์„ ๋งˆ์น˜๊ณ  ๊ด€๋ จ ์—ฐ์‚ฐ์ด ์ผ์–ด๋‚˜๊ฒŒ ํ•˜๋ ค๊ณ  ํ• ๋•Œ, transaction ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค .

import {transaction} from 'mobx'
...
transaction(()=>{
	bucket.addItem(1, "a", 20); 
	bucket.addItem(1, "a", 20); 
	bucket.addItem(1, "a", 20); 
	bucket.addItem(1, "a", 20); 
}) // 'add item' ํ•œ๋ฒˆ๋งŒ ์ฐํž˜ ! 

...

decorator

์ž๋ฐ”์—์„œ ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ๊ฐ™์€ ์ƒ๊น€์ƒˆ๋กœ decorate ๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ํŽธํžˆ ์ž…๋ ฅ ํ•  ์ˆ˜ ์žˆ๋‹ค.

import { transaction, observable, action, computed, autorun } from "mobx";

class Bucket {
  @observable bucket = [];
	
	@computed
  get total() {
    return this.bucket.reduce((acc, cur) => acc + cur.price, 0);
  }
	@action 
  addItem(id, name, price) {
    console.log("add item");
    this.bucket = [...this.bucket, { id, name, price }];
  }
}

const bucket = new Bucket();

bucket.addItem(1, "a", 20); // 'add item'
bucket.addItem(1, "a", 20); // 'add item'
bucket.addItem(1, "a", 20); // 'add item'
bucket.addItem(1, "a", 20); // 'add item'
console.log(bucket.total); // 80 

์œ„์—์„œ decorate ๋ฅผ ์“ด ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ์ด๋‹ค.

@action

class Todo {
	@observable width = 0 ; 
	constructor(){
		
	}
//	@action.bound
	@action 
	setWidth(width){
		this.width = width ;  
	}
}

์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ setwidth๋ฅผ ํ•˜์—ฌ width ๊ฐ’์„ ์„ธํŒ…ํ•˜๋ ค ํ•˜์ง€๋งŒ, ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ this๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ž˜์„œ setWidth๋ฅผ Arrow function ์œผ๋กœ ๋ฐ”๊พธ๊ฑฐ๋‚˜, @action.bound ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

React ์ ์šฉ

rolled-potatoes/study-mobx

์ฐธ๊ณ 

MobX (1) ์‹œ์ž‘ํ•˜๊ธฐ

๐Ÿ“Œ Project

๐Ÿค Rules

๐Ÿ“š Documents

๐Ÿข๊ตฌ์กฐ

๐Ÿง๋…ผ์˜

โšฝ๏ธ TroubleShooting

๐Ÿ’ฌ Memoirs

1 ์ฃผ์ฐจ
2 ์ฃผ์ฐจ
3 ์ฃผ์ฐจ
4 ์ฃผ์ฐจ
5 ์ฃผ์ฐจ

๐Ÿ“ ํ”ผ์–ด์„ธ์…˜

<2์ฃผ์ฐจ> 24๊ทธ๋ฃน 25๊ทธ๋ฃน

<3์ฃผ์ฐจ> 24๊ทธ๋ฃน 25๊ทธ๋ฃน

<4์ฃผ์ฐจ> 24๊ทธ๋ฃน 25๊ทธ๋ฃน 26๊ทธ๋ฃน 27๊ทธ๋ฃน

Clone this wiki locally