이전글 [**"\[React\] Mobx-state-tree 학습하기 #4 : Views를 사용하여 모델에서 정보 보여주기"**](/zzan/@anpigon/react-native-manage-application-state-with-mobx-state-tree-4)에서 이어지는 내용입니다. 참고로 이 포스팅은 제가 학습한 내용을 노트에 정리하듯이 기록하여 올리는 글이기 때문에 보팅 안해주셔서 됩니다. 많은 분들이 코딩에 흥미를 느꼈으면 좋겠습니다. ㅋ <br> ***  * 출처: https://egghead.io/courses/manage-application-state-with-mobx-state-tree *** <br> ## Render mobx-state-tree Models in React * 강의 링크: https://egghead.io/lessons/react-render-mobx-state-tree-models-in-react <br>5번째 레슨입니다. 이번 레슨에서는 `observer` 래퍼를 사용하여, React 컴포넌트가 자동으로 업데이트되는 과정을 배웁니다. <br>우리는 다음을 배우게 됩니다. * **mobx-react**의 옵저버(observer)를 사용하여, React 컴포넌트를 업데이트하고 재렌더링하는 방법을 알아보자. * 컴포넌트에 모델을 사용하면 컴포넌트는 멍청해진다. 그래서 가끔 stateless function components처럼 된다. * **mobx devtools**를 사용하여, mobx-react에서 자동으로 재렌더링 해야할 최소 컴포넌트 셋트를 찾는 것을 visualize하는 방법을 알아보자. <br> *** <br><br> 우선 몇가지 단일 컴포넌트를 만들어 보겠습니다. 먼저 `WishListItemView.js` 파일을 생성합니다. `src/components/WishListItemView.js` ``` import React from "react"; const WishListItemView = ({ item }) => ( <li className="item"> {item.image && <img src={item.image} alt="" />} <h3>{item.name}</h3> <span>{item.price}</span> </li> ); export default WishListItemView; ``` <br> 그다음 `components/WishListView.js` 파일을 생성합니다. `src/components/WishListView.js` ``` import React from "react"; import WishListItemView from "./WishListItemView"; const WishListView = ({ wishList }) => ( <div className="list"> <ul> {wishList.items.map((item, idx) => ( <WishListItemView key={idx} item={item} /> ))} </ul> Total: {wishList.totalPrice} 💲 </div> ); export default WishListView; ``` <br> 그리고 나서 `components/App.js` 파일을 생성합니다. `src/components/App.js` ``` import React from "react"; import WishListView from "./WishListView"; function App(props) { return ( <div> <h1 className="App-title">WishList</h1> <WishListView wishList={props.wishList} /> </div> ); } export default App; ``` <br> <br>그리고 `index.js` 파일을 수정합니다. `src/index.js` ``` import React from "react"; import ReactDOM from "react-dom"; import App from "./components/App"; import { WishList } from "./models/WhishList"; const wishList = WishList.create({ items: [ { name: "LEGO Mindstorems EV3", price: 349.95, image: "https://www.justbricks.com.au/images/thumbnails/280/229/detailed/14/31313LEGOMINDSTORMSNXTEV3.png" }, { name: "Miracles - C.S. Lewis", price: 12.91, image: "https://store.vision.org.au/5438-large_default/miracles-do-they-really-happen-c-s-lewis-paperback.jpg" } ] }); ReactDOM.render(<App wishList={wishList} />, document.getElementById("root")); ``` <br> 이제 앱을 실행하면 다음과 같이 보입니다.  <br><br> *** <br><br> ## MobX observer 사용하기 `index.js` 파일에 다음 코드를 추가합니다. 1초마다 가격에 변화를 주는 코드입니다. `src/index.js` ``` setInterval(() => { wishList.items[0].changePrice(wishList.items[0].price + 1); }, 1000); ``` 지금은 위에 코드를 입력하고 실행하면 화면에 아무런 변화가 없습니다. `observer`를 설정해서 컴포넌트의 데이터 변화를 감지해야합니다. <br> **WishListView** 모델에 `observer`를 설정합니다. `observer`는 모델의 데이터 변경을 감지하고 해당 컴포넌트를 재렌더링 할 것입니다. `src/components/WishListView.js` ``` import { observer } from 'mobx-react' // ... export default observer(WishListView); ``` <br>아래 화면에서 LEGO 가격이 증가하는 것이 보이나요?  <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-mobx-state-tree-5-react-mobx-state-tree). Steem blog powered by [ENGRAVE](https://engrave.website).</sup></center>
author | anpigon |
---|---|
permlink | react-mobx-state-tree-5-react-mobx-state-tree |
category | zzan |
json_metadata | {"format":"markdown","app":"engrave","tags":["zzan","kr","kr-dev","dev"],"image":["https://files.steempeak.com/file/steempeak/anpigon/sYISPibs-E1848CE185A6E18486E185A9E186A820E1848BE185A5E186B9E18482E185B3E186AB20E18483E185B5E1848CE185A1E1848BE185B5E186AB.png","https://files.steempeak.com/file/steempeak/anpigon/sYISPibs-E1848CE185A6E18486E185A9E186A820E1848BE185A5E186B9E18482E185B3E186AB20E18483E185B5E1848CE185A1E1848BE185B5E186AB.png","https://www.justbricks.com.au/images/thumbnails/280/229/detailed/14/31313LEGOMINDSTORMSNXTEV3.png","https://store.vision.org.au/5438-large_default/miracles-do-they-really-happen-c-s-lewis-paperback.jpg","https://files.steempeak.com/file/steempeak/anpigon/46y4Ng2j-E18489E185B3E1848FE185B3E18485E185B5E186ABE18489E185A3E186BA202019-08-182012.59.18.png","https://files.steempeak.com/file/steempeak/anpigon/XIh7Smjo-2019-08-182013-22-41.2019-08-182013_23_25.gif"],"links":["https://egghead.io/courses/manage-application-state-with-mobx-state-tree","https://egghead.io/lessons/react-render-mobx-state-tree-models-in-react","https://steemitimages.com/400x0/https://cdn.steemitimages.com/DQmQmWhMN6zNrLmKJRKhvSScEgWZmpb8zCeE2Gray1krbv6/BC054B6E-6F73-46D0-88E4-C88EB8167037.jpeg'%3E%3Ch5%3Evote,"],"users":["@anpigon"],"engrave":{"domain":"anpigon.dblog.org","permlink":"react-mobx-state-tree-5-react-mobx-state-tree","url":"https://anpigon.dblog.org/react-mobx-state-tree-5-react-mobx-state-tree","featured_image":"https://files.steempeak.com/file/steempeak/anpigon/sYISPibs-E1848CE185A6E18486E185A9E186A820E1848BE185A5E186B9E18482E185B3E186AB20E18483E185B5E1848CE185A1E1848BE185B5E186AB.png","categories":["5d8b1dc94302ae00124415b9"]}} |
created | 2019-08-24 01:51:39 |
last_update | 2019-09-30 14:35:39 |
depth | 0 |
children | 0 |
last_payout | 2019-08-31 01:51:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.557 HBD |
curator_payout_value | 0.430 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 4,300 |
author_reputation | 17,258,940,000,931 |
root_title | "[React] Mobx-state-tree 학습하기 #5 : React에서 mobx-state-tree 모델 렌더링하기" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 89,883,911 |
net_rshares | 4,242,364,302,604 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
riosparada | 0 | 185,314,919,691 | 100% | ||
cjsdns | 0 | 817,720,355,172 | 100% | ||
yoon | 0 | 111,297,793,985 | 55% | ||
vimva | 0 | 41,195,909,611 | 100% | ||
jjy | 0 | 52,247,897,367 | 100% | ||
ai-channel | 0 | 736,395,175,849 | 19% | ||
stylegold | 0 | 665,119,658 | 100% | ||
realmankwon | 0 | 6,123,387,971 | 25% | ||
beoped | 0 | 62,735,314,781 | 33% | ||
fur2002ks | 0 | 203,250,630,070 | 13% | ||
everrich | 0 | 214,670,992 | 15% | ||
ukk | 0 | 3,977,028,218 | 20% | ||
forhappywomen | 0 | 49,644,895,595 | 100% | ||
sultan-aceh | 0 | 31,800,130,434 | 2% | ||
pediatrics | 0 | 22,600,014,072 | 25% | ||
yjs3694 | 0 | 76,267,209,786 | 100% | ||
eversloth | 0 | 354,057,604,424 | 100% | ||
jacobyu | 0 | 97,885,283,606 | 100% | ||
fun2learn | 0 | 1,210,531,435 | 2% | ||
philhyuntd | 0 | 2,581,438,813 | 50% | ||
songbj | 0 | 1,542,952,318 | 100% | ||
tanky | 0 | 34,047,715,265 | 80% | ||
andrewma | 0 | 0 | 0.2% | ||
krnews | 0 | 754,068,980 | 100% | ||
ziq | 0 | 464,200,229 | 100% | ||
anpigon | 0 | 12,239,139,567 | 100% | ||
newbijohn | 0 | 18,184,103,280 | 50% | ||
wbot01 | 0 | 372,232,330 | 100% | ||
nailyourhome | 0 | 473,442,507 | 2.3% | ||
rokairforce | 0 | 1,491,289,288 | 100% | ||
georgeknowsall | 0 | 10,370,123,925 | 100% | ||
ulockblock | 0 | 65,298,224,054 | 100% | ||
cn-news | 0 | 98,506,384 | 25% | ||
china-unicom | 0 | 98,324,381 | 25% | ||
china.mobile | 0 | 1,329,750,982 | 25% | ||
erke | 0 | 98,294,569 | 25% | ||
cnpc | 0 | 98,295,444 | 25% | ||
cn-times | 0 | 98,677,720 | 25% | ||
cn-reporter | 0 | 98,494,701 | 25% | ||
mcenoramle | 0 | 2,468,447,106 | 100% | ||
elraberscer | 0 | 1,129,688,174 | 100% | ||
reportup | 0 | 48,966,512,014 | 27% | ||
delegate4upvot | 0 | 79,467,177 | 1.6% | ||
imrahelk | 0 | 21,828,311,709 | 100% | ||
doctor.strange | 0 | 375,422,310 | 100% | ||
dead.pool | 0 | 378,319,124 | 100% | ||
black.widow | 0 | 371,615,343 | 100% | ||
marvel.spiderman | 0 | 371,614,797 | 100% | ||
marvel.hulk | 0 | 372,965,990 | 100% | ||
marvel.ironman | 0 | 371,678,862 | 100% | ||
black.pan.ther | 0 | 371,685,714 | 100% | ||
mco | 0 | 970,501,089 | 11.2% | ||
honeybeerbear | 0 | 5,625,755,528 | 50% | ||
cazbu | 0 | 98,446,674 | 25% | ||
donasteem | 0 | 972,365,498 | 11.1% | ||
zyj | 0 | 98,446,674 | 25% | ||
fty | 0 | 98,424,942 | 25% | ||
dispatch | 0 | 546,145,375 | 100% | ||
gouji | 0 | 226,728,300 | 100% | ||
claim7 | 0 | 371,321,754 | 100% | ||
wcasino | 0 | 1,359,212,195 | 100% | ||
wcasino.pay | 0 | 371,461,985 | 100% | ||
wcasino.holdem | 0 | 296,052,150 | 100% | ||
wcasino.jackpot | 0 | 273,247,667 | 100% | ||
sf1 | 0 | 548,443,815 | 100% | ||
steemit.holdem | 0 | 943,421,449 | 100% | ||
steemit.jackpot | 0 | 273,311,563 | 100% | ||
deer3 | 0 | 9,695,755,459 | 25% | ||
talken | 0 | 726,615,391 | 100% | ||
steemory | 0 | 2,935,280,181 | 100% | ||
smcard | 0 | 1,259,885,631 | 100% | ||
smonsmon | 0 | 1,259,174,275 | 100% | ||
guro | 0 | 1,259,300,641 | 100% | ||
shindorim | 0 | 1,259,261,050 | 100% | ||
yongsan | 0 | 1,260,076,086 | 100% | ||
incheon | 0 | 1,259,936,712 | 100% | ||
mapo | 0 | 1,259,263,925 | 100% | ||
shingil | 0 | 935,932,385 | 100% | ||
checkname | 0 | 1,264,570,158 | 100% | ||
starterpack | 0 | 929,295,797 | 100% | ||
gdragon | 0 | 1,259,346,801 | 100% | ||
sumimasen | 0 | 1,259,688,284 | 100% | ||
smtester | 0 | 1,259,625,849 | 100% | ||
showdown | 0 | 929,296,732 | 100% | ||
monstersteem | 0 | 1,259,773,460 | 100% | ||
freesale | 0 | 1,259,452,813 | 100% | ||
freefee | 0 | 1,270,523,691 | 100% | ||
testsama | 0 | 929,415,354 | 100% | ||
kimch | 0 | 929,110,661 | 100% | ||
tongdak | 0 | 1,259,986,427 | 100% | ||
hanbok | 0 | 1,586,506,199 | 100% | ||
jjangjjangman | 0 | 929,261,734 | 100% | ||
superguard | 0 | 1,585,572,804 | 100% | ||
yawang | 0 | 929,220,633 | 100% | ||
roadmap | 0 | 929,314,705 | 100% | ||
kpay | 0 | 928,826,808 | 100% | ||
adultbaby | 0 | 1,259,587,172 | 100% | ||
sneack | 0 | 929,399,375 | 100% | ||
gzone | 0 | 1,259,269,633 | 100% | ||
ppororo | 0 | 1,259,719,879 | 100% | ||
lotto645 | 0 | 929,144,228 | 100% | ||
alphamonsters | 0 | 1,259,320,575 | 100% | ||
betamonsters | 0 | 1,259,627,337 | 100% | ||
girlfriends | 0 | 1,259,402,231 | 100% | ||
fastway | 0 | 1,259,347,965 | 100% | ||
smonsang | 0 | 929,169,960 | 100% | ||
technomart | 0 | 1,259,591,066 | 100% | ||
lastsmon | 0 | 929,450,436 | 100% | ||
postme | 0 | 1,259,399,423 | 100% | ||
smilezone | 0 | 1,271,301,021 | 100% | ||
bearbaby | 0 | 1,259,536,246 | 100% | ||
o0o0o | 0 | 1,281,264,219 | 100% | ||
thecards | 0 | 1,263,335,469 | 100% | ||
developments | 0 | 1,259,316,307 | 100% | ||
originals | 0 | 1,259,585,724 | 100% | ||
beanpole | 0 | 1,259,656,045 | 100% | ||
oilbank | 0 | 1,259,256,444 | 100% | ||
iliili | 0 | 1,259,341,420 | 100% | ||
kotlin | 0 | 1,259,319,059 | 100% | ||
flutters | 0 | 1,259,271,748 | 100% | ||
prettyguy | 0 | 1,259,269,080 | 100% | ||
gamemonsters | 0 | 1,259,132,465 | 100% | ||
blueguy | 0 | 687,973,312 | 100% | ||
sicbo | 0 | 1,345,823,145 | 100% | ||
yaoi | 0 | 1,355,534,974 | 100% | ||
farmfarm | 0 | 1,354,152,394 | 100% | ||
giantroc | 0 | 1,023,694,184 | 100% | ||
koboldminer | 0 | 1,023,693,920 | 100% | ||
crustaceanking | 0 | 1,023,761,247 | 100% | ||
waterelemental | 0 | 1,023,528,035 | 100% | ||
goblinsorcerer | 0 | 1,023,788,468 | 100% | ||
ragingimpaler | 0 | 1,025,211,612 | 100% | ||
animatedcorpse | 0 | 1,355,038,867 | 100% | ||
spiritforest | 0 | 1,023,954,294 | 100% | ||
serpentflame | 0 | 1,023,747,941 | 100% | ||
goblincaptain | 0 | 1,023,757,279 | 100% | ||
lyannaforest | 0 | 1,023,792,675 | 100% | ||
divineknight | 0 | 1,023,881,029 | 100% | ||
feralwarrior | 0 | 1,023,690,992 | 100% | ||
elementalair | 0 | 1,023,889,828 | 100% | ||
jestertwisted | 0 | 1,038,668,456 | 100% | ||
bansheescreaming | 0 | 1,023,750,478 | 100% | ||
skyselenia | 0 | 1,023,751,952 | 100% | ||
darknesslord | 0 | 1,023,951,287 | 100% | ||
lightangel | 0 | 1,023,944,820 | 100% | ||
naturalyanna | 0 | 1,024,178,157 | 100% | ||
astormbringer | 0 | 1,023,813,253 | 100% | ||
giantfrost | 0 | 1,023,986,754 | 100% | ||
warriorminotaur | 0 | 1,025,259,730 | 100% | ||
golemalric | 0 | 1,023,847,021 | 100% | ||
orcelemental | 0 | 1,354,442,393 | 100% | ||
spiritpriest | 0 | 1,023,882,942 | 100% | ||
lordjester | 0 | 1,023,995,510 | 100% | ||
magifirestorm | 0 | 1,023,621,108 | 100% | ||
muhan | 0 | 1,023,368,426 | 100% | ||
pigoncchio | 0 | 535,075,657 | 100% | ||
smseller | 0 | 1,595,010,873 | 100% | ||
skysung | 0 | 354,599,675 | 50% | ||
j-car | 0 | 112,321,302,841 | 7% | ||
coredump | 0 | 104,834,436 | 10% | ||
son1001 | 0 | 800,883,260 | 100% | ||
minigame | 0 | 880,238,159,742 | 6% | ||
deepdown | 0 | 546,323,527 | 100% | ||
terarosa | 0 | 546,228,606 | 100% | ||
hakeznel | 0 | 545,852,212 | 100% | ||
sct.cu9 | 0 | 10,058,699,380 | 50% | ||
sct.cu7 | 0 | 8,924,531,493 | 100% | ||
happy-banguri | 0 | 1,173,750,091 | 10% | ||
wangpigon | 0 | 1,157,556,079 | 100% | ||
lucky2.aaa | 0 | 512,577,486 | 100% | ||
lovelyyeon.cur | 0 | 941,724,987 | 100% | ||
nympheas | 0 | 457,432,307 | 100% | ||
zzan.biz | 0 | 1,315,012,063 | 100% | ||
zzan.co3 | 0 | 545,047,943 | 100% | ||
zzan.co10 | 0 | 151,582,623 | 50% | ||
herobear | 0 | 128,535,512 | 100% | ||
zzan.co12 | 0 | 607,076,822 | 100% | ||
kudock | 0 | 328,716,253 | 100% | ||
zzan.co13 | 0 | 161,489,516 | 50% | ||
wonsama.zzan | 0 | 1,574,895,561 | 100% | ||
astraea09 | 0 | 430,725,184 | 100% | ||
butterdog | 0 | 362,074,469 | 100% | ||
zzangu | 0 | 2,333,566,250 | 50% | ||
feelsogood.zz0 | 0 | 630,925,865 | 100% | ||
realmankwon.scot | 0 | 1,080,094,074 | 100% | ||
hodolbak-zzan | 0 | 2,069,205,340 | 100% | ||
cn-sct | 0 | 1,047,500,465 | 2% | ||
gghite.zzan | 0 | 1,599,178,983 | 100% | ||
onealfa.zzan | 0 | 602,471,979 | 90.64% | ||
sct1004 | 0 | 825,802,174 | 25% | ||
hykwf678233 | 0 | 238,434,138 | 100% | ||
done.mod | 0 | 421,307,878 | 100% | ||
zzan.blue | 0 | 209,292,772 | 100% |