-
Notifications
You must be signed in to change notification settings - Fork 128
[리뷰 요청] 2주차 1번 과제 #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: ctaaag
Are you sure you want to change the base?
[리뷰 요청] 2주차 1번 과제 #185
Changes from 6 commits
2ce7408
25da462
7423a20
ea12862
c246ad8
0168378
db9870e
a378bf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import React, { useState } from 'react'; | ||
| import ClickMeButton from './components/ClickMeButton.jsx'; | ||
| import NumberButtons from './components/NumberButtons.jsx'; | ||
|
|
||
| export default function App() { | ||
| const [counterNumber, setCounterNumber] = useState(0); | ||
|
|
||
| function handlerClickButton({ number }) { | ||
| setCounterNumber(counterNumber + number); | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <p>Counter</p> | ||
| <ClickMeButton | ||
| counterNumber={counterNumber} | ||
| handlerClickButton={handlerClickButton} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handler함수의 이름과 컴포넌트의 속성 이름을 똑같이 맞춰주신 것 같아요. 핸들러 함수에 function handleClick() {
alert('버튼이 클릭됐어요!');
}
<Button onClick={handleClick} />위의 예제를 보면 보면
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 핸들러 함수에 handle*이라고 이름을 붙이는 이유는 특정 이벤트가 발생했을 때 그 이벤트를 처리하는 함수라는 맥락은 이해가 되었고, 그래서 on으로 시작하는 이름으로 전달하는 의미도 이해는 되었습니다! 그런데 위의 경우에서 자식요소에서 handleClick이라는 함수를 쓸 때는 아래와 같이 사용하게 될 것 같은데요! 이렇게 될 경우에는 onClick이라는 것을 onClick으로 전달받은 함수를 실행한다는 의미인데 그럼에도 on으로 시작하는 이름으로 전달해주는 것이다라는 의미를 강조하는 목적이 더 크니 이렇게 쓰는게 더 나은 방식이라고 이해하면 될까요?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 컴포넌트에서 onClick이라는 이름으로 함수를 실행하니 어색하게 느껴질 수 있겠네요. 사용하는 컴포넌트에서 사용하는게 어색하게 느껴지신다면 이름을 바꿔서 사용할 수 있긴 하겠네요. const ButtonComponent = ({ onClick: handleClick }) => {
return (
<button onClick={handleClick}></button>
);
};저는 컴포넌트를 쓰는 입장에서 이해하기 좋은 이름이어야 한다는 생각때문에 그랬던 것 같습니다 ㅎㅎ
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하! 제안해주신 것도 좋은 방법인것같아요! |
||
| /> | ||
| <br /> | ||
| <NumberButtons handlerClickButton={handlerClickButton} /> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function ClickMeButton({ counterNumber, handlerClickButton }) { | ||
| return ( | ||
| <button type="button" onClick={() => handlerClickButton({ number: 1 })}> | ||
| Click me ( | ||
| {counterNumber} | ||
| ) | ||
| </button> | ||
| ); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 버튼을 클릭했을 때 1씩 증가해서 만약 이러한 로직을 외부에서 변경할 수 있도록 하려면 어떻게 해야 할까요
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
이런식으로 컴포넌트 생성할 때 whatNumberToAdd라는 값도 props로 넘겨주고
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 별도의 속성을 넣어서 값을 몇씩 더할지 변경할 수 있겠네요 ㅎㅎ 좋습니다. 저는 핸들러 함수를 외부에서 전달하고 컴포넌트에서는 단순히 실행만 하도록 하는 것을 생각해 보았어요 const handleClickMeButton = () => {
setCounerNumber(counterNumber + 10);
}
return (
<ClickMeButton>
counterNumber={counterNumber}
onClick={handleClickMeButton}>
</ClickMeButton>
)
//
export default function ClickMeButton({ counterNumber, onClick }) {
return (
<button type="button" onClick={onClick}>
Click me (
{counterNumber}
)
</button>
);
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하! 좋은 것 같아요 :) 저 같은 경우에는 handleClickButton이라는 핸들러함수를 clickmebutton 컴포넌트와 numberbutton에 공통으로 사용되는 함수로 두고 싶었어요! 피드백해주셔서 감사해요 :) |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function NumberButtons({ handlerClickButton }) { | ||
| return [1, 2, 3, 4, 5].map((i) => ( | ||
| <button type="button" onClick={() => handlerClickButton({ number: i })}> | ||
| {i} | ||
| </button> | ||
| )); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import App from './App'; | ||
|
|
||
| ReactDOM.render(<App />, document.getElementById('app')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이렇게 확장자 없이 작성해 보세요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://baeharam.netlify.app/posts/lint/Lint-ESLint-+-Prettier-%EC%84%A4%EC%A0%95%ED%95%98%EA%B8%B0
eslint-plugin-react 설치 여부를 체크하고 위의 룰을 추가로 기입해주면 해결된다는 사례도 있어서
해봤는데 이 역시도 안되네요 🥺