[React] Mobx-state-tree 학습하기 #8 : Create an Entry Form to Add Models to the State Tree

avatar
(Edited)

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





Create an Entry Form to Add Models to the State Tree


우리는 다음을 배우게 됩니다.

  • 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


Originally posted on 안피곤님의 블로그. Steem blog powered by ENGRAVE.



0
0
0.000
6 comments
avatar

kr-newbie님이 anpigon님을 멘션하셨습니당. 아래 링크를 누르시면 연결되용~ ^^
kr-newbie님의 [8/26 kr-newbie 큐레이팅 프로젝트]

...b.io/#/wallet/kr-newbie
happigon에서 자세한 임대내역이 확인 가능합니다.
만들어주신 anpigon께 감사드립니다!
  • anpigon님의 HAAPIGON 유저가이드를 참고해주시기 바랍니다!

    0
    0
    0.000
    avatar

    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.

    0
    0
    0.000
    avatar

    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

    0
    0
    0.000
    avatar

    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.js file


    and add this code

    import 'bootstrap/scss/bootstrap.scss'
    import 'antd/dist/antd.css'; // or 'antd/dist/antd.less'
    


    like this.


    is it right?

    0
    0
    0.000
    avatar

    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:
    • You're on the right track, try to gather more followers.
    • The readers like your work!
    • Great user engagement! You rock!

    Feel free to join our @steem-ua Discord server

    0
    0
    0.000