이전글 ["\[React\] Mobx-state-tree 학습하기 #14 : 서버에서 데이터 가져오기"](/zzan/@anpigon/react-mobx-state-tree-14)에서 이어지는 내용입니다. 참고로 이 포스팅은 제가 학습한 내용을 노트에 정리하듯이 기록하여 올리는 글이기 때문에 보팅 안해주셔서 됩니다. 많은 분들이 코딩에 흥미를 느꼈으면 좋겠습니다. ㅋ <br> ***  * 출처: https://egghead.io/courses/manage-application-state-with-mobx-state-tree *** <br> ## Use Volatile State and Lifecycle Methods to Manage Private State * 강의 링크: https://egghead.io/lessons/react-use-volatile-state-and-lifecycle-methods-to-manage-private-state <br>15번째 레슨입니다. MST에는 매우 유니크한 기능이 있습니다. It allows you to capture private state on models, and manage this state by using lifecycle hooks. For example by setting up a WebSocket connection and disposing of the connection automatically as soon as the instance gets removed from the store. In this lesson, we will leverage cancellable fetches to abort in-flight requests when appropriate <br>우리는 이번 레슨에서 다음을 배우게 됩니다. - `window.fetch` 요청을 중단하는 방법. - Storing private, volatile, internal state in the function closure - A second life-cycle hook: `beforeDestroy` <br> *** <br> 이번에는 새로고침 버튼을 추가하고, 새로고침 버튼을 누를 때마다 서버에서 데이터를 받아오도록 해보겠습니다. <br> `Group.js` 파일을 수정합니다. Group 모델에 reload 액션을 추가합니다. `src/models/Group.js` ```js export const Group = types.model({ users: types.map(User) }) .actions(self => ({ // (...) // reload 액션 추가 reload() { self.load(); }, })); ``` <br> 그리고 화면에 Reload 버튼을 추가합니다. Reload을 누르면 group의 reaload 액션을 호출할 것입니다. `src/components/App.js` ```js class App extends React.Component { // (...) render() { // (...) return ( <div> <h1 className="App-title">WishList</h1> <button onClick={group.reload}>Reload</button> ``` <br> 이제 서버에서 데이터를 다시 로드 할 수 있습니다. 개발자 도구에서 네트워크 요청을 살펴보면 데이터를 멋지게 가져 오는 것을 볼 수 있습니다. 사실 문제가 하나 있습니다. 리로드 버튼을 많이 클릭하면 많은 요청이 진행됩니다. 다시 요청할 때 이전 요청을 취소하면 좋을 것입니다. 새로운 요청이 들어오면 현재 요청은 중단되어야합니다. Fetch API에는 이를 지원하는 라이브러리가 있습니다. 이를 수행하는 방법은 AbortController를 작성하는 것입니다. fetch를 생성할 때 signal를 전달합니다. AbortController에서 나온 signal을 Fetch API에 바인딩합니다. 이제 AbortController에서 abort을 호출하면 Fetch API가 취소됩니다. 요청이 중단되면 에러가 발생할 것입니다. try / catch를 사용하여 요청이 중단되었음을 알 수 있습니다. 그렇지 않은 경우에만 성공으로 처리합니다. 다음과 같이 **AbortController** 를 사용합니다. `src/models/Group.js` ```js export const Group = types .model({ users: types.map(User) }) .actions(self => { let controller; // 컨트롤러 return { afterCreate() { self.load(); }, // load 액션 수정 load: flow(function*() { controller = window.AbortController && new window.AbortController(); try { const response = yield window.fetch("http://localhost:3001/users", { signal: controller.signal }); applySnapshot(self.users, yield response.json()); console.log("success"); } catch (e) { console.log("aborted", e.name); } }), reload() { // abort current request if (controller) controller.abort(); self.load(); } }; }); ``` <br>그리고 어떤 이유에서 group이 메모리에서 언로드되면 우리는 보류중인 요청을 중단 할 수 있어야 합니다. 여기에는 beforeDestroy 후크를 활용할 수 있습니다. ```js export const Group = types .model({ users: types.map(User) }) .actions(self => { let controller; return { // (...) beforeDestory() { if (controller) controller.abort(); } }); ``` <br> 이제 많은 요청을 실행하면 마지막 요청만 완료되고 다른 요청들은 중단됩니다. 콘솔 로그를 확인하면 이것이 우리가 기대했던 동작을 확인할 수 있습니다. 새로운 요청이 발생하여 일부는 성공하고 일부는 중단된 것을 볼 수 있습니다.  <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 <a href='https://www.steemzzang.com/@anpigon'>@anpigon</a></h5></center> *** <center><sup>Originally posted on [안피곤님의 블로그](http://anpigon.dblog.org/react-mobx-state-tree-15-use-volatile-state-and-lifecycle-methods-to-manage-private-state). Steem blog powered by [ENGRAVE](https://engrave.website).</sup></center>
author | anpigon |
---|---|
permlink | react-mobx-state-tree-15-use-volatile-state-and-lifecycle-methods-to-manage-private-state |
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","https://files.steempeak.com/file/steempeak/anpigon/Zqr3LQV5-2.gif"],"links":["https://egghead.io/courses/manage-application-state-with-mobx-state-tree","https://egghead.io/lessons/react-use-volatile-state-and-lifecycle-methods-to-manage-private-state","http://localhost:3001/users","https://steemitimages.com/400x0/https://cdn.steemitimages.com/DQmQmWhMN6zNrLmKJRKhvSScEgWZmpb8zCeE2Gray1krbv6/BC054B6E-6F73-46D0-88E4-C88EB8167037.jpeg'%3E%3Ch5%3Evote,","https://steemzzang.com/@anpigon'%3E@anpigon%3C/a%3E%3C/h5%3E%3C/center%3E"],"engrave":{"domain":"anpigon.dblog.org","permlink":"react-mobx-state-tree-15-use-volatile-state-and-lifecycle-methods-to-manage-private-state","url":"https://anpigon.dblog.org/react-mobx-state-tree-15-use-volatile-state-and-lifecycle-methods-to-manage-private-state","featured_image":"https://files.steempeak.com/file/steempeak/anpigon/sYISPibs-E1848CE185A6E18486E185A9E186A820E1848BE185A5E186B9E18482E185B3E186AB20E18483E185B5E1848CE185A1E1848BE185B5E186AB.png","categories":["5d8b1dc94302ae00124415b9"]},"community":"busy","good":true} |
created | 2019-09-01 05:11:45 |
last_update | 2019-09-30 14:31:36 |
depth | 0 |
children | 2 |
last_payout | 2019-09-08 05:11:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.559 HBD |
curator_payout_value | 0.542 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 4,347 |
author_reputation | 17,258,940,000,931 |
root_title | "[React] Mobx-state-tree 학습하기 #15 : Use Volatile State and Lifecycle Methods to Manage Private State" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 90,160,271 |
net_rshares | 4,067,076,935,362 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
guest123 | 0 | 740,488,934 | 100% | ||
busy.pay | 0 | 697,645,271,569 | 3.35% | ||
sisilafamille | 0 | 483,054,472 | 100% | ||
happyberrysboy | 0 | 33,358,133,411 | 100% | ||
hellosteem | 0 | 663,298,834 | 100% | ||
ai-channel | 0 | 679,208,634,051 | 19% | ||
stylegold | 0 | 715,973,725 | 100% | ||
virus707 | 0 | 231,322,733,699 | 1% | ||
realmankwon | 0 | 5,493,614,265 | 25% | ||
beoped | 0 | 50,299,859,533 | 24% | ||
dodoim | 0 | 3,972,687,949 | 100% | ||
ukk | 0 | 3,950,947,365 | 20% | ||
olorin | 0 | 312,814,726,147 | 50% | ||
forhappywomen | 0 | 49,886,087,458 | 100% | ||
omerbiabdulah | 0 | 469,748,210 | 100% | ||
krfeed | 0 | 228,941,182 | 100% | ||
pediatrics | 0 | 24,077,792,455 | 25% | ||
eversloth | 0 | 333,601,784,456 | 100% | ||
fun2learn | 0 | 1,224,879,273 | 2% | ||
philhyuntd | 0 | 2,585,117,911 | 50% | ||
songbj | 0 | 1,566,642,913 | 100% | ||
rubberducky1004 | 0 | 331,902,999 | 100% | ||
blockchainstudio | 0 | 184,516,153,676 | 100% | ||
anpigon | 0 | 38,479,551,995 | 100% | ||
newbijohn | 0 | 19,099,731,583 | 50% | ||
nailyourhome | 0 | 399,968,509 | 2.4% | ||
rokairforce | 0 | 2,462,698,890 | 100% | ||
ulockblock | 0 | 31,252,002,369 | 100% | ||
china.mobile | 0 | 1,418,385,942 | 25% | ||
steeming-hot | 0 | 0 | 0.01% | ||
bluengel | 0 | 1,686,882,126 | 100% | ||
steemnara | 0 | 402,800,000 | 100% | ||
reportup | 0 | 81,287,722,767 | 27% | ||
delegate4upvot | 0 | 86,062,417 | 1.6% | ||
imrahelk | 0 | 23,075,756,330 | 100% | ||
honeybeerbear | 0 | 5,058,168,947 | 50% | ||
talken | 0 | 604,495,582 | 100% | ||
steemory | 0 | 2,577,838,246 | 100% | ||
pigoncchio | 0 | 441,344,270 | 100% | ||
maeil | 0 | 0 | 100% | ||
paraquets | 0 | 262,941,305 | 100% | ||
skysung | 0 | 358,764,113 | 50% | ||
j-car | 0 | 111,356,670,043 | 7% | ||
coredump | 0 | 90,387,312 | 10% | ||
blockchain.app | 0 | 0 | 100% | ||
son1001 | 0 | 1,080,270,417 | 100% | ||
minigame | 0 | 1,084,801,697,125 | 7% | ||
booksinsteem | 0 | 0 | 100% | ||
buksteem | 0 | 0 | 100% | ||
sct.cu16 | 0 | 22,664,010,695 | 100% | ||
happy-banguri | 0 | 403,271,006 | 10% | ||
wangpigon | 0 | 1,242,375,420 | 100% | ||
lucky2.aaa | 0 | 1,597,850,849 | 100% | ||
lovelyyeon.cur | 0 | 933,576,360 | 100% | ||
nympheas | 0 | 397,269,587 | 100% | ||
zzan.biz | 0 | 2,047,202,548 | 100% | ||
zzan.co1 | 0 | 218,881,727 | 50% | ||
zzan.co3 | 0 | 560,508,617 | 100% | ||
zzan.co7 | 0 | 514,949,827 | 100% | ||
zzan.co10 | 0 | 230,616,542 | 50% | ||
herobear | 0 | 359,199,536 | 100% | ||
zzan.co12 | 0 | 591,212,173 | 100% | ||
kudock | 0 | 369,606,655 | 100% | ||
zzan.co13 | 0 | 230,760,411 | 51% | ||
zzan.co20 | 0 | 408,530,411 | 100% | ||
astraea09 | 0 | 461,442,349 | 100% | ||
zzangu | 0 | 2,541,149,543 | 51% | ||
realmankwon.scot | 0 | 1,075,401,688 | 100% | ||
gghite.zzan | 0 | 1,716,896,270 | 100% | ||
boftea | 0 | 462,724,481 | 100% | ||
hykwf678233 | 0 | 870,437,069 | 100% | ||
zzan.blue | 0 | 286,378,917 | 100% | ||
curation.neo | 0 | 569,676,532 | 100% | ||
bruzza | 0 | 346,353,486 | 100% | ||
news90 | 0 | 534,037,888 | 100% |
 [wonsama.zzan](/@wonsama.zzan)님이 anpigon님을 멘션하셨습니당. 아래 링크를 누르시면 연결되용~ ^^ <br />[wonsama.zzan](/@wonsama.zzan)님의 [[ZZAN] DAILY REPORT 2019-08-31](/zzan/@wonsama.zzan/zzan-daily-report-2019-08-31) <br /> <blockquote>...td> <td>MORNING VERSE IN JES</td> <td>15</td> </tr> <tr> <td>anpigon/td> <td>15:54:27</td> <td>[React] Mobx-state-t</td> <td>20</...</blockquote>
author | steem.apps |
---|---|
permlink | re---------react-mobx-state-tree-15-use-volatile-state-and-lifecycle-methods-to-manage-private-state-20190901t062242013z |
category | zzan |
json_metadata | {"app":"steem.apps/0.1","format":"markdown"} |
created | 2019-09-01 06:22:42 |
last_update | 2019-09-01 06:22:42 |
depth | 1 |
children | 0 |
last_payout | 2019-09-08 06:22: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 | 450 |
author_reputation | 7,218,006,883,278 |
root_title | "[React] Mobx-state-tree 학습하기 #15 : Use Volatile State and Lifecycle Methods to Manage Private State" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 90,161,511 |
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-mobx-state-tree-15-use-volatile-state-and-lifecycle-methods-to-manage-private-state-1567339437 |
category | zzan |
json_metadata | {"tags":["jjm"]} |
created | 2019-09-01 12:03:48 |
last_update | 2019-09-01 12:03:48 |
depth | 1 |
children | 0 |
last_payout | 2019-09-08 12:03:48 |
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 학습하기 #15 : Use Volatile State and Lifecycle Methods to Manage Private State" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 90,169,115 |
net_rshares | 0 |