inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[React 1부] 만들고 비교하며 학습하는 React

[순수JS 1] 검색결과 2(실습)

reset() 함수 작성 관련 문의드립니다.

344

JIYEON KIM

작성한 질문수 4

0

안녕하세요.

저는 아래와 같이 Controller에서는 Store의 reset()함수를 호출만하고, Store에서 상태를 변경하는 방식으로 작성했는데요.

// Controller.js

export default class Controller {
  constructor(store, { searchFormView, searchResultView }) {
    this.store = store;

    this.searchFormView = searchFormView;
    this.subscribeViewEvents();
  }

  subscribeViewEvents() {
    this.searchFormView //
      .on("@reset", () => this.reset());
  }

  reset() {
    console.log(tag, "reset");
    this.store.reset(); // 작성한 부분
    this.render();
  }
}
// Store.js

export default class Store {
  constructor(storage) {
    console.log(tag, "constructor");

    if (!storage) throw "no storage";

    this.storage = storage;

    this.searchKeyword = "";
    this.searchResult = [];
  }

  // 작성한 부분
  reset() {
    this.searchKeyword = "";
    this.searchResult = [];
  }
}

강사님 풀이는 다음과 같이, Controller에서 store의 상태값을 직접 넣어주는 식으로 작성을 하셨더라구요.

// Controller.js

...

reset() {
    console.log(tag, "reset");

    this.store.searchKeyword = "";
    this.store.searchResult = [];
    this.render();
  }

둘 다 기능상의 차이는 없는 것 같은데, 혹시 둘 중 선호되는 방식이 있는지 궁금합니다.

react MVC

답변 1

3

김정환

저는 컨트롤러에서 직접 스토어 내부 변수를 초기화했는데요. 이렇게 스토어 메소드로 분리하니깐 역할분담이 더 잘된것 같네요. 저는 JIYEON KIM 님 코드가 더 좋습니다.

0

JIYEON KIM

답변 감사합니다!

리액트 1,2부 이후 후속 강의나 준비 중인 다른 강의가 있으신가요?

1

90

2

super.show() 호출하는 이유가 궁금합니다.

1

81

2

class와 constructor를 이용한 객체 지향 프로그래밍

0

224

2

mvc 패턴 질문

0

197

2

Cannot read properties of undefined (reading 'props')

0

271

2

delegate, emit 필요한 이유

0

197

2

어떤거를 먼저 들어야 하는지 도와주세요

1

265

1

localhost:8080 접속 불가

1

282

1

최근검색어 3 풀이에서

1

188

1

import 문제

1

232

1

자동변환 관련

1

203

1

sort() 질문 드립니다.

1

304

2

reset 시점에 searchResult 빈 배열로 update

2

311

2

Button에 Onclick사용

1

247

1

npx lite-server error 확인 요청드립니다.

1

470

2

node 20이상 쓸때 꿀팁

4

759

3

디버깅 관련질문이 있습니다.

1

295

1

[순수JS2]탭 3(풀이) 질문있습니다.

1

229

1

추상화를 어떻게 받아들이면 될까요??

1

435

1

on 메서드 eventName 문의

1

266

1

import에 관해서 질문드립니다

1

255

1

view.js의 on 메소드에 대해 질문드립니다

1

259

1

componentDidMount에서 getKeywordList()를 하는 이유

1

242

1

[순수JS1]검색폼2_git branch 이동관련

1

280

1