๋ฐ์ํ
๐ช ์์ ์ ์ฝ๋
writeComment = e => {
this.setState({
comment: e.target.value,
});
};
addComment = e => {
โญ๏ธ e.preventDefault();
const { commentList, comment } = this.state;
this.setState({
commentList: [
...commentList,
{
id: commentList.length + 1,
userName: 'zzz_yk',
content: comment,
},
],
comment: '',
});
};
enterKey = e => {
if (e.key === 'Enter') {
this.addComment();
}
};
<div className="reply">
<input
type="text"
id="input"
value={comment}
placeholder="๋๊ธ ๋ฌ๊ธฐ..."
onChange={this.writeComment}
onKeyPress={this.enterKey}
/>
<button
type="button"
className="reply-btn"
onClick={this.addComment}
>
๊ฒ์
</button>
</div>
์ด๋ ๊ฒ ํ๊ณ ๋๊ธ์ ์ฐ๊ณ Enter๋ฅผ ์น๋ฉด!!
์ด๋ฐ ๋ฉ์ธ์ง๊ฐ ๋ฌ๋ค!!!ใ ใ
๊ทธ๋์ <input>์ onkeyPress๋ฅผ ์์ ๊ณ input ํ๊ทธ๋ฅผ <form> ํ๊ทธ๋ก ๊ฐ์ธ์ฃผ์๋ค!
๐ฅ e.preventDefault()๋ก submit ์ด๋ฒคํธ ๋ฐ์์ reload ์ํ๊ธฐhtml์์ a ํ๊ทธ๋ submit ํ๊ทธ๋ ๊ณ ์ ์ ๋์์ผ๋ก ํ์ด์ง๋ฅผ ์ด๋์ํค๊ฑฐ๋, form ์์ input ๋ฑ์ ์ ์กํ๋ ๋์์ด ์๋๋ฐ e.preventDefault()๋ ๊ทธ ๋์์ ์ค์ง์ํค๋ ์ญํ ์ ํ๋ค.<input> ๋๋ <button> ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ํ์ด์ง๊ฐ reload ๋๋๋ฐ, ๊ทธ ํ์์ ๋ง์์ค!!! ์ฐธ๊ณ ๐๐ป event.preventDefault |
๐ช ์์ ํ ์ฝ๋
writeComment = e => {
this.setState({
comment: e.target.value,
});
};
addComment = e => {
e.preventDefault();
const { commentList, comment } = this.state;
this.setState({
commentList: [
...commentList,
{
id: commentList.length + 1,
userName: 'zzz_yk',
content: comment,
},
],
comment: '',
});
};
// enterKey = e => {
// if (e.key === 'Enter') {
// this.addComment();
// }
// };
<div className="reply">
<form onSubmit={this.addComment}>
<input
type="text"
id="input"
value={comment}
placeholder="๋๊ธ ๋ฌ๊ธฐ..."
onChange={this.writeComment}
/>
</form>
<button
type="button"
className="reply-btn"
onClick={this.addComment}
>
๊ฒ์
</button>
</div>
์ preventDefault(); ๊ฐ ์์ผ๋ฉด... ๋๊ธ์ ์
๋ ฅํ๋ฉด ๋ฐ๋ก reload๊ฐ ๋๋ฒ๋ฆผ!!!
์ ๋์ค๋๊ตฌ๋ง...ํํํ
๋ฐ์ํ
'๋ด๊ฐ ๋ง๋ ERROR' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Vue] Error: Avoided redundant navigation to current location. (0) | 2021.11.24 |
---|---|
[Golang] go module error (0) | 2021.11.17 |
[React] Each child in a list should have a unique key prop. (0) | 2021.11.16 |
[React] Invalid DOM property `class`. Did you mean `className`? (0) | 2021.11.16 |
[React] Too many re-renders. React limits the number of renders to prevent an infinite loop. (0) | 2021.11.16 |
๋๊ธ