react create구현

2020. 5. 27. 00:29· dev/react

참고사이트

www.youtube.com/playlist?list=PLuHgQVnccGMCRv6f8H9K5Xwsdyg4sFSdi

 

React - YouTube

 

www.youtube.com

 

App.js추가 <Controller 추가

import Control from './components/Control';

...생략
return (
...생략

<Control onChangeMode={function(_mode){
          this.setState({
            mode:_mode
          })
        }.bind(this)}></Control>
);

 

Control.js (component추가)

create, update, delete 추가해서 호출에 따라서 mode값 변경

import React, { Component } from 'react';

class Control extends Component {
    render(){
      return (
        <ul>
          <li><a href="/create" onClick={function(e){
            e.preventDefault();
            this.props.onChangeMode('create');
          }.bind(this)}>create</a></li>
          <li><a href="/update" onClick={function(e){
            e.preventDefault();
            this.props.onChangeMode('update');
          }.bind(this)}>update</a></li>
          <li><input type="button" value="delete" onClick={function(e){
            e.preventDefault();
            this.props.onChangeMode('delete');
          }.bind(this)}></input></li>
        </ul>
      );
    }
  }

export default Control;

 

-ReadArticle.js 추가 

create 클릭시 보여질 화면

import React, { Component } from 'react';

class ReadArticle extends Component {
    render(){
      return (
        <article>
            <h2>{this.props.title}</h2>
            {this.props.sub} <br/>
        </article>
      );
    }
  }

  export default ReadArticle;

 

- create 클릭시 변경되는 로직 추가 

mode에 따라서 _article 변경되게 수정

var _article 추가

_article = <ReadArticle title={_title} sub={_desc}></ReadArticle> 추가 

{_article} 추가 

import React, { Component } from 'react';
import Subject from "./components/Subject";
import Nav from "./components/Nav";
import ReadArticle from "./components/ReadArticle";
import CreateArticle from "./components/CreateArticle";
import './App.css';
import Control from './components/Control';

class App extends Component {
  //state 초기화
  //컴포넌트가 실행될때 constructor이 있다면 제일먼저 실행되어서 초기화를 담당한다.
  constructor(props){
    super(props);
    //subject state값을 만듬
    this.state = {
      mode: 'welcome',
      selected_content_id: 2,
      welcome: {title:'welcome', desc:'welcome react!'},
      subject:{title:'WEB', sub:'World Wide Web!'},
      contents:[
        {id:1, title:'HTML', desc: 'HTML is for information'},
        {id:2, title:'CSS', desc: 'CSS is for design'},
        {id:3, title:'JavaScript', desc: 'JavaScript is for interactive'},
      ]
    }
  }

  render(){
    var _title, _desc, _article = null;
    if (this.state.mode === 'welcome'){
      _title = this.state.welcome.title
      _desc = this.state.welcome.desc
      _article = <ReadArticle title={_title} sub={_desc}></ReadArticle>
    } else if (this.state.mode === 'read'){

      var i = 0;
      while(i < this.state.contents.length){
        var data = this.state.contents[i];
        if(this.state.selected_content_id === data.id){
          _title = data.title;
          _desc = data.desc;
          break;
        }
        i = i + 1;
      }
      _article = <ReadArticle title={_title} sub={_desc}></ReadArticle>
    } else if(this.state.mode === 'create'){
      _article = <CreateArticle title={_title} sub={_desc}></CreateArticle>
    }
    console.log('render', this);
    return (
      <div className="App">
        <Subject 
          title={this.state.subject.title} 
          sub={this.state.subject.sub}
          onChangePage={
            function(){
              //alert('hihihi');
              this.setState({mode: 'welcome'});
            }.bind(this)}
        >
        </Subject>

        <Nav 
          onChangePage={
            function(id){
              //alert('abc');
              this.setState({
                mode: 'read',
                selected_content_id: Number(id)
              });
            }.bind(this)
          }
          data={this.state.contents}
        ></Nav>
        <Control onChangeMode={function(_mode){
          this.setState({
            mode:_mode
          })
        }.bind(this)}></Control>
        {_article}
      </div>
    );
  }
}

export default App;

 

'dev > react' 카테고리의 다른 글

react props, state 개념정리  (0) 2020.05.21
react 컴포넌트이벤트 만들기  (0) 2020.05.19
react 이벤트  (0) 2020.05.17
react state소개 및 사용  (0) 2020.05.15
react 속성(props), react Developer Tools 사용  (0) 2020.05.13
'dev/react' 카테고리의 다른 글
  • react props, state 개념정리
  • react 컴포넌트이벤트 만들기
  • react 이벤트
  • react state소개 및 사용
아디봉
아디봉
내인생의 카드는 몇장일까?
내이른내인생의 카드는 몇장일까?
아디봉
내이른
아디봉
전체
오늘
어제
  • 분류 전체보기 (81)
    • 경제적자유 (11)
      • 경제노트 (4)
      • 부동산노트 (3)
      • 책리뷰 (1)
      • 유튜브 (2)
    • dev (50)
      • 사이드프로젝트 (0)
      • JavaScript (7)
      • 스프링 (5)
      • 자바 (11)
      • docker (7)
      • tdd (0)
      • DB (2)
      • mongoDB (1)
      • python (1)
      • react (9)
      • 인증.권한 (1)
      • 기타 (5)
      • HTTP (0)
      • JSP (1)
    • life (9)
      • 백패킹 (3)
      • 자기개발 (6)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 파코기
  • 옵셔널
  • 객체
  • react 이벤트
  • jstree
  • 파란코끼리들의기적
  • Object
  • 정다르크
  • 백패킹
  • Java8
  • Lombok
  • function
  • 스프링데이터jpa
  • React
  • Maven
  • javascript
  • 쿠키
  • javascript jsTree
  • Java
  • docker
  • Eclipse
  • Cookie
  • 자바스크립트
  • 롬복
  • web
  • Oracle
  • 자바
  • log4j
  • optional
  • logback

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.2
아디봉
react create구현
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.