본문 바로가기
웹서비스 개발/React-App 개발

Update, Delete 구현

by 후닝훈 2021. 5. 19.
반응형

UpdateContend.js

import React, { Component } from 'react';

class UpdateContent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            id: this.props.data.id,
            title: this.props.data.title,
            desc: this.props.data.desc
        }
        this.inputFormHandler = this.inputFormHandler.bind(this);
    }
    inputFormHandler(e) {
        this.setState({ [e.target.name]: e.target.value });
    }
    render() {
        console.log(this.props.data);
        console.log('UpdateContent render');
        return (
            <article>
                <h2>Update</h2>
                <form action="/create_process" method="post"
                    onSubmit={function (e) {
                        e.preventDefault();
                        this.props.onSubmit(
                            this.state.id,
                            this.state.title,
                            this.state.desc
                        );
                    }.bind(this)}
                >
                    <input type="hidden" name="id" value={this.state.id}></input>
                    <p>
                        <input
                            type="text"
                            name="title"
                            placeholder="title"
                            value={this.state.title}
                            onChange={this.inputFormHandler}
                        ></input>
                    </p>
                    <p>
                        <textarea
                            onChange={this.inputFormHandler}
                            name="desc"
                            placeholder="description"
                            value={this.state.desc}></textarea>
                    </p>
                    <p>
                        <input type="submit"></input>
                    </p>
                </form>
            </article>
        );
    }
}

export default UpdateContent;

Delete는 App.js에 추가해야 한다.

 if(_mode === 'delete'){
            if(window.confirm('really?')){
              var _contents = Array.from(this.state.contents);
              var i = 0;
              while(i < _contents.length){
                if(_contents[i].id === this.state.selected_content_id){
                  _contents.splice(i,1);
                  break;
                }
                i = i + 1;
              }
              this.setState({
                mode:'welcome',
                contents:_contents
              });
              alert('deleted!');
            }
          } else {
            this.setState({
              mode:_mode
            });
          }

 

최종결과물

Create

Update & Delete

반응형

'웹서비스 개발 > React-App 개발' 카테고리의 다른 글

Create 구현 - 메소드  (0) 2021.05.19
Create 구현 - contents 변경  (0) 2021.05.18
Create 구현 - form, onSubmit  (0) 2021.05.17
Create 구현 - Mode 전환  (0) 2021.05.10
Props & State 의 차이  (0) 2021.05.04

댓글