이전글 [**"\[React\] Mobx-state-tree 학습하기 #3 : Snapshots 또는 Patches를 Recording하여 mobx-state-tree 모델 테스트하기"**](/zzan/@anpigon/react-native-manage-application-state-with-mobx-state-tree-3)에서 이어지는 내용입니다. 참고로 이 포스팅은 제가 학습한 내용을 노트에 정리하듯이 기록하여 올리는 글이기 때문에 보팅 안해주셔서 됩니다. 많은 분들이 코딩에 흥미를 느꼈으면 좋겠습니다. ㅋ <br> ***  * 출처: https://egghead.io/courses/manage-application-state-with-mobx-state-tree *** <br> # Derive Information from Models Using Views > 강의 링크: https://egghead.io/lessons/react-derive-information-from-models-using-views 4번째 레슨입니다. 이번 레슨에서는 데이터를 선언적으로 derive하고 캐싱하는 views 사용 방법에 대해 학습합니다. <br>우리는 다음을 배우게 됩니다. * 모델에서 views를 introduce하는 방법. * 계산된 값(computed properties)은 Mobx computed fields에 의해 작동. * `reaction ` 처럼 MST를 Mobx 유틸리티와 결합(combine)하는 방법 <br> *** <br> WhishList 모델에 총 가격(totalPrice) 필드를 추가합니다. `src/models/WhishList.js` ``` const WishList = types .model({ items: types.optional(types.array(WishListItem), []), totalPrice: types.number, // add here }) ``` 하지만 이렇게 사용하면 WishList 모델이 변경될 때마다 `totalPrice`를 매번 계산해야합니다. 그래서 다음과 같이 views를 사용합니다. views 이미 계산된 값을 캐싱합니다. 그래서 `item`이 추가되거나 `price` 가 변경되지 않으면 `totalPrice`를 다시 계산하지 않고 캐싱하고 있는 값을 리턴합니다. ``` export const WishList = types .model({ items: types.optional(types.array(WishListItem), []) // totalPrice: types.number, }) .views(self => ({ get totalPrice() { return self.items.reduce((sum, entry) => sum + entry.price, 0); } })) ``` <br><br> # 모델의 view 테스트 하기 다음과 같이 `WhishList.test.js`에 테스트 케이스를 작성합니다. `src/models/WhishList.test.js` ``` it("can calculate the total price of a wishlist", () => { const list = WishList.create({ items: [ { name: "Chesterton", price: 7.35 }, { name: "Book of G.K. Chesterton", price: 349.95 } ] }); expect(list.totalPrice).toBe(357.3); }); ``` <br><br> # reaction 사용하여 모델의 view 테스트 하기 mobx에서 제공하는 `reaction` 유틸은 모델의 데이터 변화를 감지합니다. 모델에 변화가 발생하였을때 `totalPrice` 값의 변화를 살펴봅시다. ``` import { reaction } from 'mobx'; it("can calculate the total price of a wishlist", () => { // ... let changed = 0; // totalPrice 변화가 발생하면 changed 증가 reaction(() => list.totalPrice, () => changed++); expect(changed).toBe(0); // 변화 없음 console.log(list.totalPrice); list.items[0].changeName("Test"); // 이름 변경 expect(changed).toBe(0); // 변화 없음 list.items[0].changePrice(10); // 가격 변경 expect(changed).toBe(1); // 계산됨 }); ``` 위 테스트를 수행해보면 `price` 값에 변화가 발생하였을때만, `totalPrice`가 동작하는 것을 확인할 수 있습니다. <br>오늘 수업 끝. *** <br> <br> `댓글`, `팔로우`, `좋아요` 해 주시는 모든 분께 감사합니다. 항상 행복한 하루 보내시길 바랍니다. *** <center><img src='https://steemitimages.com/400x0/https://cdn.steemitimages.com/DQmQmWhMN6zNrLmKJRKhvSScEgWZmpb8zCeE2Gray1krbv6/BC054B6E-6F73-46D0-88E4-C88EB8167037.jpeg'><h5>vote, reblog, follow <code><a href='/@anpigon'>@anpigon</a></code></h5></center> *** <center><sup>Originally posted on [안피곤님의 블로그](http://anpigon.dblog.org/react-native-manage-application-state-with-mobx-state-tree-4). Steem blog powered by [ENGRAVE](https://engrave.website).</sup></center>
author | anpigon |
---|---|
permlink | react-native-manage-application-state-with-mobx-state-tree-4 |
category | zzan |
json_metadata | {"format":"markdown","app":"busy/2.5.6","tags":["zzan","kr","kr-dev","dev","busy","jjm"],"image":["https://files.steempeak.com/file/steempeak/anpigon/sYISPibs-E1848CE185A6E18486E185A9E186A820E1848BE185A5E186B9E18482E185B3E186AB20E18483E185B5E1848CE185A1E1848BE185B5E186AB.png","https://files.steempeak.com/file/steempeak/anpigon/sYISPibs-E1848CE185A6E18486E185A9E186A820E1848BE185A5E186B9E18482E185B3E186AB20E18483E185B5E1848CE185A1E1848BE185B5E186AB.png"],"links":["https://egghead.io/courses/manage-application-state-with-mobx-state-tree","https://egghead.io/lessons/react-derive-information-from-models-using-views","https://steemitimages.com/400x0/https://cdn.steemitimages.com/DQmQmWhMN6zNrLmKJRKhvSScEgWZmpb8zCeE2Gray1krbv6/BC054B6E-6F73-46D0-88E4-C88EB8167037.jpeg'%3E%3Ch5%3Evote,"],"engrave":{"domain":"anpigon.dblog.org","permlink":"react-native-manage-application-state-with-mobx-state-tree-4","url":"https://anpigon.dblog.org/react-native-manage-application-state-with-mobx-state-tree-4","featured_image":"https://files.steempeak.com/file/steempeak/anpigon/sYISPibs-E1848CE185A6E18486E185A9E186A820E1848BE185A5E186B9E18482E185B3E186AB20E18483E185B5E1848CE185A1E1848BE185B5E186AB.png","categories":["5d8b1dc94302ae00124415b9"]},"community":"busy","good":true} |
created | 2019-08-20 02:29:36 |
last_update | 2019-09-30 14:37:48 |
depth | 0 |
children | 5 |
last_payout | 2019-08-27 02:29:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 1.195 HBD |
curator_payout_value | 0.342 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3,282 |
author_reputation | 17,258,940,000,931 |
root_title | "[React] Mobx-state-tree 학습하기 #4 : Views를 사용하여 모델에서 정보 보여주기" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,725,841 |
net_rshares | 4,702,065,572,769 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
bert0 | 0 | 1,235,262,944 | 0.4% | ||
guest123 | 0 | 862,302,223 | 100% | ||
busy.pay | 0 | 637,409,190,117 | 3.07% | ||
sudutpandang | 0 | 1,268,548,090 | 100% | ||
sisilafamille | 0 | 565,757,585 | 100% | ||
happyberrysboy | 0 | 22,307,835,009 | 100% | ||
hellosteem | 0 | 772,895,824 | 100% | ||
ai-channel | 0 | 715,468,577,825 | 19% | ||
stylegold | 0 | 615,699,707 | 100% | ||
virus707 | 0 | 241,008,939,561 | 1% | ||
realmankwon | 0 | 5,676,773,998 | 25% | ||
dodoim | 0 | 2,856,759,587 | 100% | ||
fur2002ks | 0 | 189,133,957,419 | 13% | ||
ukk | 0 | 3,248,108,903 | 20% | ||
forhappywomen | 0 | 47,085,158,642 | 100% | ||
accelerator | 0 | 17,301,159,194 | 0.54% | ||
omerbiabdulah | 0 | 503,295,799 | 100% | ||
krfeed | 0 | 272,510,688 | 100% | ||
pediatrics | 0 | 20,394,594,192 | 25% | ||
yjs3694 | 0 | 83,406,790,131 | 100% | ||
goodhello | 0 | 29,451,008,363 | 100% | ||
eversloth | 0 | 309,712,417,221 | 100% | ||
fun2learn | 0 | 1,059,521,085 | 2% | ||
philhyuntd | 0 | 2,558,763,642 | 50% | ||
rainingfall | 0 | 888,986,195 | 10% | ||
asinayo | 0 | 55,249,238,459 | 100% | ||
songbj | 0 | 1,555,374,324 | 100% | ||
wonsama | 0 | 41,327,839,803 | 100% | ||
hhusaini | 0 | 87,175,458,464 | 30% | ||
rubberducky1004 | 0 | 266,029,429 | 100% | ||
leems | 0 | 2,421,838,807 | 100% | ||
tanky | 0 | 31,227,286,841 | 75% | ||
ernaerningsih | 0 | 128,830,212 | 30% | ||
andrewma | 0 | 0 | 0.2% | ||
steem.apps | 0 | 3,819,513,080 | 25% | ||
blockchainstudio | 0 | 185,272,425,247 | 100% | ||
ziq | 0 | 661,748,212 | 100% | ||
kibumh | 0 | 55,592,338,908 | 25% | ||
anpigon | 0 | 1,584,053,805 | 100% | ||
newbijohn | 0 | 19,002,067,155 | 50% | ||
muhammadhasan2 | 0 | 118,128,436 | 30% | ||
esteemkedua | 0 | 117,817,613 | 30% | ||
hhusaini1 | 0 | 824,315,102 | 30% | ||
nailyourhome | 0 | 393,932,423 | 2.8% | ||
rokairforce | 0 | 1,115,826,371 | 100% | ||
ulockblock | 0 | 150,042,927,646 | 100% | ||
cn-news | 0 | 98,415,702 | 25% | ||
china-unicom | 0 | 98,233,809 | 25% | ||
china.mobile | 0 | 1,396,666,650 | 25% | ||
erke | 0 | 98,204,015 | 25% | ||
cnpc | 0 | 98,204,890 | 25% | ||
cn-times | 0 | 97,993,285 | 25% | ||
cn-reporter | 0 | 98,404,025 | 25% | ||
mcenoramle | 0 | 2,365,934,537 | 100% | ||
elraberscer | 0 | 1,081,477,182 | 100% | ||
moby.dick | 0 | 5,996,026,172 | 100% | ||
steemnara | 0 | 435,527,629 | 100% | ||
laissez-faire | 0 | 74,725,388 | 100% | ||
reportup | 0 | 48,657,315,217 | 27% | ||
delegate4upvot | 0 | 100,238,632 | 1.8% | ||
imrahelk | 0 | 22,445,863,803 | 100% | ||
mco | 0 | 1,031,727,381 | 12% | ||
honeybeerbear | 0 | 4,872,952,576 | 50% | ||
cazbu | 0 | 97,763,303 | 25% | ||
donasteem | 0 | 852,068,342 | 9.9% | ||
neuralnetwork | 0 | 580,310,527 | 100% | ||
zyj | 0 | 97,763,303 | 25% | ||
fty | 0 | 97,742,700 | 25% | ||
gouji | 0 | 233,033,066 | 100% | ||
sf1 | 0 | 542,546,152 | 100% | ||
deago | 0 | 144,984,573 | 30% | ||
talken | 0 | 812,827,629 | 100% | ||
steemory | 0 | 3,219,563,847 | 100% | ||
crowdwisdom | 0 | 29,106,626 | 100% | ||
pigoncchio | 0 | 600,915,237 | 100% | ||
maeil | 0 | 0 | 100% | ||
curatoris | 0 | 540,919,240 | 100% | ||
skysung | 0 | 347,525,439 | 50% | ||
j-car | 0 | 111,357,923,480 | 7% | ||
son1001 | 0 | 854,024,490 | 100% | ||
minigame | 0 | 844,586,793,904 | 6% | ||
fortress | 0 | 539,748,990 | 100% | ||
deepdown | 0 | 540,656,466 | 100% | ||
terarosa | 0 | 540,540,171 | 100% | ||
hakeznel | 0 | 540,206,016 | 100% | ||
stpeople | 0 | 444,773,610,151 | 8.06% | ||
plim | 0 | 6,495,735,859 | 30% | ||
bodum | 0 | 538,269,089 | 100% | ||
roadofrich | 0 | 184,573,151,178 | 1.13% | ||
wangpigon | 0 | 1,236,537,716 | 100% | ||
map10k | 0 | 3,816,714,678 | 1.1% | ||
lucky2.aaa | 0 | 623,870,361 | 100% | ||
lovelyyeon.cur | 0 | 829,139,448 | 100% | ||
nympheas | 0 | 416,547,961 | 100% | ||
atomy.indonesia | 0 | 10,036,619,972 | 5% | ||
zzan.biz | 0 | 1,334,415,695 | 100% | ||
zzan.adv | 0 | 433,852,472 | 25% | ||
zzan.co3 | 0 | 555,205,849 | 100% | ||
bungong.aceh | 0 | 303,091,433 | 30% | ||
zzan.co7 | 0 | 252,334,587 | 100% | ||
zzan.co10 | 0 | 124,682,506 | 50% | ||
herobear | 0 | 236,371,055 | 100% | ||
langet | 0 | 227,532,474 | 30% | ||
kudock | 0 | 316,389,905 | 100% | ||
zzan.co13 | 0 | 158,425,734 | 50% | ||
zzan.co20 | 0 | 270,426,489 | 100% | ||
wonsama.zzan | 0 | 1,515,742,587 | 100% | ||
atomy.indo | 0 | 593,679,341 | 30% | ||
astraea09 | 0 | 399,788,380 | 100% | ||
zzangu | 0 | 4,468,401,705 | 100% | ||
realmankwon.scot | 0 | 975,090,966 | 100% | ||
hodolbak-zzan | 0 | 2,265,894,047 | 100% | ||
cn-sct | 0 | 980,753,002 | 2% | ||
gghite.zzan | 0 | 2,219,587,369 | 100% | ||
windmolen | 0 | 789,148,651 | 100% | ||
onealfa.zzan | 0 | 175,976,071 | 31.65% | ||
hykwf678233 | 0 | 378,919,478 | 100% | ||
done.mod | 0 | 299,929,023 | 100% | ||
zzan.blue | 0 | 283,058,862 | 100% |
응원해주시는 모든 분들께 감사합니다. ㅎㅎ
author | anpigon |
---|---|
permlink | pwixj3 |
category | zzan |
json_metadata | {"tags":["zzan"],"app":"steemzzang/0.1"} |
created | 2019-08-20 07:16:15 |
last_update | 2019-08-20 07:16:15 |
depth | 1 |
children | 0 |
last_payout | 2019-08-27 07:16:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 23 |
author_reputation | 17,258,940,000,931 |
root_title | "[React] Mobx-state-tree 학습하기 #4 : Views를 사용하여 모델에서 정보 보여주기" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,732,492 |
net_rshares | 0 |
 [kr-newbie](/@kr-newbie)님이 anpigon님을 멘션하셨습니당. 아래 링크를 누르시면 연결되용~ ^^ <br />[kr-newbie](/@kr-newbie)님의 [[8/20 kr-newbie 큐레이팅 프로젝트]](/zzan/@kr-newbie/8-20-kr-newbie) <br /> <blockquote>...b.io/#/wallet/kr-newbie happigon에서 자세한 임대내역이 확인 가능합니다. 만들어주신 anpigon께 감사드립니다! <ul> <li>anpigon님의 HAAPIGON 유저가이드를 참고해주시기 바랍니다!</...</blockquote>
author | steem.apps |
---|---|
permlink | re---------react-native-manage-application-state-with-mobx-state-tree-4-20190820t110615244z |
category | zzan |
json_metadata | {"app":"steem.apps/0.1","format":"markdown"} |
created | 2019-08-20 11:06:15 |
last_update | 2019-08-20 11:06:15 |
depth | 1 |
children | 0 |
last_payout | 2019-08-27 11:06:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 414 |
author_reputation | 7,218,006,883,278 |
root_title | "[React] Mobx-state-tree 학습하기 #4 : Views를 사용하여 모델에서 정보 보여주기" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,739,269 |
net_rshares | 3,800,257,037 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
steem.apps | 0 | 3,800,257,037 | 25% |
 [wonsama.zzan](/@wonsama.zzan)님이 anpigon님을 멘션하셨습니당. 아래 링크를 누르시면 연결되용~ ^^ <br />[wonsama.zzan](/@wonsama.zzan)님의 [[ZZAN] DAILY REPORT 2019-08-20](/zzan/@wonsama.zzan/zzan-daily-report-2019-08-20) <br /> <blockquote>...td> <td>[Movie 리뷰] 숨은 진주같은 한</td> <td>28</td> </tr> <tr> <td>anpigon/td> <td>11:29:36</td> <td>[React] Mobx-state-</td> <td>6</t...</blockquote>
author | steem.apps |
---|---|
permlink | re---------react-native-manage-application-state-with-mobx-state-tree-4-20190820t153606028z |
category | zzan |
json_metadata | {"app":"steem.apps/0.1","format":"markdown"} |
created | 2019-08-20 15:36:06 |
last_update | 2019-08-20 15:36:06 |
depth | 1 |
children | 0 |
last_payout | 2019-08-27 15:36:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 450 |
author_reputation | 7,218,006,883,278 |
root_title | "[React] Mobx-state-tree 학습하기 #4 : Views를 사용하여 모델에서 정보 보여주기" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,751,506 |
net_rshares | 0 |
 [blockchainstudio](/@blockchainstudio)님이 anpigon님을 멘션하셨습니당. 아래 링크를 누르시면 연결되용~ ^^ <br />[zzan1004](/@zzan1004)님의 [ALL IN ONE CLAIM](/zzan/@zzan1004/all-in-one-claim#@blockchainstudio/re-zzan1004-all-in-one-claim-20190820t154857784z) <br /> <blockquote> 이번에도 anpigon 멋진 기능 추가하셨군요. 사실 관련포맷나왔을때 스엔에서 알아서 기능을 넣을 줄 알았는데 아직도 안되고 있었군요ㅠㅠ 참고로 이전에 scotauto도 한번에 처리하는 방식으로 바뀌었습니다. 참고로 딜레이는 ...</blockquote>
author | steem.apps |
---|---|
permlink | re---------react-native-manage-application-state-with-mobx-state-tree-4-20190820t154907318z |
category | zzan |
json_metadata | {"app":"steem.apps/0.1","format":"markdown"} |
created | 2019-08-20 15:49:09 |
last_update | 2019-08-20 15:49:09 |
depth | 1 |
children | 0 |
last_payout | 2019-08-27 18:47:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 487 |
author_reputation | 7,218,006,883,278 |
root_title | "[React] Mobx-state-tree 학습하기 #4 : Views를 사용하여 모델에서 정보 보여주기" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,751,891 |
net_rshares | 0 |
Thank you for your continued support towards JJM. For each 1000 JJM you are holding, you can get an additional 1% of upvote. 10,000JJM would give you a 11% daily voting from the 700K SP virus707 account.
author | virus707 |
---|---|
permlink | re-react-native-manage-application-state-with-mobx-state-tree-4-1566324109 |
category | zzan |
json_metadata | {"tags":["jjm"]} |
created | 2019-08-20 18:01:48 |
last_update | 2019-08-20 18:01:48 |
depth | 1 |
children | 0 |
last_payout | 2019-08-27 18:47:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 203 |
author_reputation | 557,563,606,581,756 |
root_title | "[React] Mobx-state-tree 학습하기 #4 : Views를 사용하여 모델에서 정보 보여주기" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,756,206 |
net_rshares | 0 |