inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

생활코딩 - React

State 소개

컴포넌트 2번 호출되는 문제

168

qkrthfk

작성한 질문수 10

0

console로그를 확인해보니 

각 컴포넌트들이 두번 씩 호출되는데 왜 그런지 알 수 있을까요?

import React, { Component } from 'react';
import TOC from './components/TOC';
import Content from './components/Content';
import Subject from './components/Subject';
import './App.css';

class App extends Component{
  constructor(props){
    super(props);
    this.state = ({
      mode      : 'welcome',
      subject   : { title : "WEB", sub   : "World wide web!"},
      welcome   : { title : "Welcome", desc   : "Hello, React!!"},
      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() {
    console.log('App.js render');
    var _title, _desc = null;
    if(this.state.mode === 'welcome'){
      _title = this.state.welcome.title;
      _desc = this.state.welcome.desc;
    } else if(this.state.mode === 'read'){
      _title = this.state.contents[0].title;
      _desc = this.state.contents[0].desc;
    }

    return (
      <div className="App">
          <Subject title={this.state.subject.title} sub={this.state.subject.sub}></Subject>
          <TOC data={this.state.contents}></TOC>
          <Content title={_title} desc={_desc}></Content>
      </div>      
    );
  }
}
export default App;


react

답변 0

2022 개정판

0

343

1

20.3 update에서 state변경이 안되는지 변경이 안됩니다

1

300

1

9강 배포하는법

0

277

1

19.6 create 구현 : contents 변경 강의에서 create가 안됩니다.

0

396

1

에바잖아요 이거

0

1109

1

1강부터 빠짐없이 따라했는데 10강의 pure.html이 안돼요

2

716

2

VS Code - 전체 주석처리 어떻게 하나요? 안되네요ㅠㅠㅠㅠ

0

2016

1

TOC.js에서 while문 안에

0

406

1

react 폴더 생성 이후 문제가 생겼습니다

0

481

0

10강에서

1

437

1

chrome에서 화면 전환이 안됩니다...

0

434

1

반복문으로 li태그 내용 입력하기

0

350

0

npm run start 했는데 에러 뜨시는 분들 참고하세요~

5

491

0

변수에 null을 넣는 이유는 뭘까요?

1

502

1

this.props.data가 어디서 나오는거죠?

0

578

1

빌드 관련 질문

0

261

0

크롬 웹스토어의 리액트툴 링크입니다.

3

295

0

class type 소스코드 예시

5

1057

3

쿵짝쿵짝

0

271

0

npm start 아무리 해도 해결이 안되요 ㅠㅠ

0

455

1

update구현:state변경 8분이후

0

309

1

original data 를 변경하지 않는 목적에 대해서

0

197

0

리액트 없이도 html 에서 include 쓰면...?

0

248

0

현재는 comunity에 tools라는 항목이 없네요

1

293

1