Skip to content

Conversation

@sooojinan
Copy link

안수진

1Week homework

this.state = {
number: 0
number: 0,
number2: 0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number와 number2로 나누어 작성해주셨습니다. 잘 작성해주셨습니다.

다만 이런 고민을 해보면 좀 더 좋은 코드에 대해 생각해볼 수 있을 것 같습니다. 만약 카운터가 3개가 된다면, 각각의 함수들이 한개씩 더 늘어나게 됩니다. 이를 줄이려면 어떻게 해야할까요?

보다 근본적으로는 app 에서 모든 state를 갖고 있음으로 인해 발생하게되는 문제이기도 합니다. counter가 state를 갖게 하는 방식으로 해결방법을 고민해 봅시다.

handleIncreaseDouble={this.handleIncreaseDouble}
handleDecreaseDouble={this.handleDecreaseDouble}
handleReset2={this.handleReset2}
number2={this.state.number2}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어차피 모든 함수들을 counter로 넘겨줄거라면, 애초에 보다 낮은 컴포넌트인 counter에서 각각의 함수들을 정의하는 것이 보다 직관적일 것 같습니다.

<div>값: {this.props.number2}</div>
<button onClick={this.props.handleIncreaseDouble}>+</button>
<button onClick={this.props.handleDecreaseDouble}>-</button>
<button onClick={this.props.handleReset2}>reset</button>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

카운터 1과 카운터 는 사실상 같은 일을 하지만, 늘어나는 숫자의 차이만 존재합니다. 이 두 컴포넌트간의 차이에 해당하는 부분만 props로 받도록 하고 나머지는 같은 컴포넌트를 사용하는 방식이 보다 효율적입니다.
예를들어 이런 방식으로요. 자세한 내용은 답지를 참고해주세요.

Copy link
Owner

@yesjin-git yesjin-git left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다. 카운터를 잘 구현해주셨습니다. 코드의 재사용성과 state의 위치에 대해 좀더 개선할 수 있는 포인트들이 있을 것 같습니다. 관련된 내용은 커맨트 남겨두었습니다. 자세한 내용은 답지를 참고해주세요.

점수는 B+입니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants