[React] Mobx-state-tree 학습하기 #8 : Create an Entry Form to Add Models to the State Tree
(Edited)
이전글 "[React] Mobx-state-tree 학습하기 #7 : Remove Model Instances from the Tree"에서 이어지는 내용입니다. 참고로 이 포스팅은 제가 학습한 내용을 노트에 정리하듯이 기록하여 올리는 글이기 때문에 보팅 안해주셔서 됩니다. 많은 분들이 코딩에 흥미를 느꼈으면 좋겠습니다. ㅋ

Create an Entry Form to Add Models to the State Tree
- 강의 링크: https://egghead.io/lessons/react-create-an-entry-form-to-add-models-to-the-state-tree
8번째 레슨입니다. 위시 리스트에 새 항목을 추가 할 시간입니다. 우리는 지금까지 작성한 입력폼과 모델을 재사용할 것입니다.
우리는 다음을 배우게 됩니다.
- MST는 단일 상태 트리(single state tree)로 제한되지 않습니다. 모든 모델은 자체적인 트리를 가질 수 있습니다
- 상태 트리(state tree)에 모델 인스턴스 추가하기
새 파일 WishListItemEntry.js을 생성합니다. 여기서 WishListItemEdit 컴포넌트를 재활용합니다. 그리고 Add 버튼을 만들었습니다. 입력양식을 작성하고 Add 버튼을 누르면 WishList 모델에 새 항목을 추가하게 됩니다.
src/components/WishListItemEntry.js
import React, { Component } from "react";
import { observer } from "mobx-react";
import WishListItemEdit from "./WishListItemEdit";
import { WishListItem, WishList } from "../models/WhishList";
class WishListItemEntry extends Component {
constructor() {
super();
this.state = {
entry: WishListItem.create({
name: "",
price: 0
})
};
}
render() {
return (
<div>
<WishListItemEdit item={this.state.entry} />
<button onClick={this.onAdd}>Add</button>
</div>
);
}
onAdd = () => {
this.props.wishList.add(this.state.entry);
this.setState({
entry: WishListItem.create({
name: "",
price: 0
})
});
};
}
export default WishListItemEntry;
그다음 WishListView.js 파일을 수정합니다. WishListView 컴포넌트에는 방금 만든 WishListItemEntry 컴포넌트를 추가하여 화면에 보여줍니다.
src/components/WishListView.js
import React from "react";
import { observer } from "mobx-react";
import WishListItemView from "./WishListItemView";
import WishListItemEntry from './WishListItemEntry'; // add here
const WishListView = ({ wishList }) => (
<div className="list">
<ul>
{wishList.items.map((item, idx) => (
<WishListItemView key={idx} item={item} />
))}
</ul>
Total: {wishList.totalPrice} 💲
<WishListItemEntry wishList={wishList} />
</div>
);
export default observer(WishListView);
실행화면
이제 새로운 항목을 작성하고 Add 버튼을 눌러보세요.

오늘 수업 끝.
댓글, 팔로우, 좋아요 해 주시는 모든 분께 감사합니다.
항상 행복한 하루 보내시길 바랍니다.

vote, reblog, follow @anpigon
0
0
0.000
kr-newbie님의 [8/26 kr-newbie 큐레이팅 프로젝트]
Congratulations @anpigon!
You raised your level and are now a Dolphin!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
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.
Hi, how are you doing? I have some tasks on Github related to the marlians.com tribe. I wonder if you will have time to take a look and perhaps help to carry it out.
https://github.com/steem-engine-exchange/nitrous/issues/94
https://github.com/steem-engine-exchange/nitrous/issues/98
https://github.com/steem-engine-exchange/nitrous/issues/93
If you need to chat, you can find me here: https://discord.gg/Jjhrej3
I'm using Google Translator so it's hard to chat in English.
So I can't help you much.
but I can help this one.
Just edit
src/app/components/pages/Grow.jsfileand add this code
like this.
is it right?
Hi @anpigon!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.420 which ranks you at #7424 across all Steem accounts.
Your rank has not changed in the last three days.
In our last Algorithmic Curation Round, consisting of 115 contributions, your post is ranked at #20.
Evaluation of your UA score:
Feel free to join our @steem-ua Discord server