인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

cleo525's profile image
cleo525

asked

[React Part 1] Learning React by making and comparing

[Pure JS 1] Search Results 2 (Practice)

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

Written on

·

324

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();
  }

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

reactMVC

Answer 1

3

jeonghwan님의 프로필 이미지
jeonghwan
Instructor

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

cleo525님의 프로필 이미지
cleo525
Questioner

답변 감사합니다!

cleo525's profile image
cleo525

asked

Ask a question