๐ ๋ฆฌ์กํธ๋ก ๊ตฌ๊ตฌ๋จ ๋ง๋ค๊ธฐ
โ this.state
: ํ๋ฉด์์ ํด๋ฆญ ์ ๋ฐ๋๊ฑฐ๋ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ธ์ ๋ ๋ฐ๋๋ ๊ฒ๋ค
์์ ๊ฐ์ด ์ ๋ ฅ ๋ฒํผ์ ํด๋ฆญํ๊ธฐ ์ ๊ณผ ํ์ ๋ฐ๋๋ ์์๋ค์ this.state ์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
1. first
2. second
3. value
4. result
constructor(props) {
super(props);
// this.state ์ ๋ฐ๋๋ ์์๋ค์ ๋ํด ์ ์ผ๋ฉด ๋๋ค.
// ํ๋ฉด์์ ํด๋ฆญ ์ ๋ฐ๋๊ฑฐ๋ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ธ์ ๋ ๋ฐ๋๋ ๊ฒ๋ค (boolean ์์๋ค๊ณผ ๊ฐ์)
this.state = {
first: Math.ceil(Math.random() * 9), // ์ฒซ๋ฒ ์งธ ์ซ์ : ์์์ ์๋ฌด ์ซ์๋ ๋ง๋ค๊ธฐ
second: Math.ceil(Math.random() * 9), // ๋๋ฒ ์งธ ์ซ์ : ์์์ ์๋ฌด ์ซ์๋ ๋ง๋ค๊ธฐ
value: '', // ์
๋ ฅ์ฐฝ์ ๋ค์ด๊ฐ ๊ฐ (์ฒ์์๋ ์๋ฌด ๊ฐ๋ ์๊ธฐ ๋๋ฌธ์ ''๋ก ์ ์ด์ค๋ค.)
result: '', // ๊ฒฐ๊ณผ ๊ฐ (์ฒ์์๋ ์๋ฌด ๊ฐ๋ ์๊ธฐ ๋๋ฌธ์ ''๋ก ์ ์ด์ค๋ค.)
};
}
// ์ฌ๊ธฐ์ {} ์์๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ๋ฃ์ ์ ์๋ค.
render() {
return (
<div>
<div>{this.state.first} ๊ณฑํ๊ธฐ {this.state.second} ๋?</div>
<form onSubmit={(e) => {
e.preventDefault();
if (parseInt(value) === this.state.first * this.state.second) {
this.setState({
result: '์ ๋ต',
first: Math.ceil(Math.random() * 9),
second: Math.ceil(Math.random() * 9),
value: '',
});
} else {
this.setState({
result: '๋ก',
value: '',
});
}
}}>
<input type="number" value={this.state.value} onChange={(e) => this.setState({ value: e.target.value })} />
<button>์
๋ ฅ!</button>
</form>
<div>{this.state.result}</div>
</div>
);
}
๊ทผ๋ฐ jsx ์ javascript ๋ฅผ ์์ด์ ์ฐ๋ฉด ์ ๋๋ค.
onSubmit = (e) => {
e.preventDefault();
if (parseInt(value) === this.state.first * this.state.second) {
this.setState({
result: '์ ๋ต',
first: Math.ceil(Math.random() * 9),
second: Math.ceil(Math.random() * 9),
value: '',
});
} else {
this.setState({
result: '๋ก',
value: '',
});
}
}
onSubmit ์ ์ด๋ ๊ฒ ํจ์๋ก ๋นผ์ฃผ๊ณ
<form onSubmit={this.onSubmit}>
return ์์๋ ์ด๋ ๊ฒ ๊ฐ๋จํ๊ฒ ๋ฃ์ด์ค๋ค. ๊ทธ๋ฌ๋ฉด ์ฝ๋๊ฐ ํจ์ฌ ๊ฐ๊ฒฐํด์ง๋ค.
<input type="number" value={this.state.value}
onChange={(e) => this.setState({value: e.target.value})}/>
input ํ๊ทธ์ ๋ค์ด์๋ onChange ๋ ํจ์๋ก ๋นผ์ค๋ค.
โ ์ฝ๋ ์ ๋ฆฌ
<div id="root"></div>
<script type="text/babel">
class GuGuDan extends React.Component {
constructor(props) {
super(props);
// this.state ์ ๋ฐ๋๋ ์์๋ค์ ๋ํด ์ ์ผ๋ฉด ๋๋ค.
// ํ๋ฉด์์ ํด๋ฆญ ์ ๋ฐ๋๊ฑฐ๋ ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ธ์ ๋ ๋ฐ๋๋ ๊ฒ๋ค (boolean ์์๋ค๊ณผ ๊ฐ์)
this.state = {
first: Math.ceil(Math.random() * 9), // ์ฒซ๋ฒ ์งธ ์ซ์ : ์์์ ์๋ฌด ์ซ์๋ ๋ง๋ค๊ธฐ
second: Math.ceil(Math.random() * 9), // ๋๋ฒ ์งธ ์ซ์ : ์์์ ์๋ฌด ์ซ์๋ ๋ง๋ค๊ธฐ
value: '', // ์
๋ ฅ์ฐฝ์ ๋ค์ด๊ฐ ๊ฐ (์ฒ์์๋ ์๋ฌด ๊ฐ๋ ์๊ธฐ ๋๋ฌธ์ ''๋ก ์ ์ด์ค๋ค.)
result: '', // ๊ฒฐ๊ณผ ๊ฐ (์ฒ์์๋ ์๋ฌด ๊ฐ๋ ์๊ธฐ ๋๋ฌธ์ ''๋ก ์ ์ด์ค๋ค.)
};
}
onSubmit = (e) => {
e.preventDefault();
if (parseInt(this.state.value) === this.state.first * this.state.second) {
// ์
๋ ฅํ ๊ฐ๊ณผ, ์ค์ ๊ฒฐ๊ณผ๊ฐ ๊ฐ๋ค๋ฉด ๊ฐ๋ค์ด ์ด๋ป๊ฒ ๋ฐ๋์ง
this.setState({
result: '์ ๋ต',
first: Math.ceil(Math.random() * 9),
second: Math.ceil(Math.random() * 9),
value: '',
});
} else {
this.setState({
result: '๋ก!!!!!!!!!!!!!!!!!!!',
value: '',
});
}
}
onChange = (e) => {
this.setState({ value: e.target.value })
}
// ์ฌ๊ธฐ์ {} ์์๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ๋ฃ์ ์ ์๋ค.
render() {
return (
<div>
<div>{this.state.first} ๊ณฑํ๊ธฐ {this.state.second} ๋?</div>
<form onSubmit={this.onSubmit}>
<input type="number" value={this.state.value}
onChange={this.onChange}/>
<button>์
๋ ฅ!</button>
</form>
<div>{this.state.result}</div>
</div>
);
}
}
</script>
<script type="text/babel">
ReactDOM.render(<GuGuDan/>, document.querySelector('#root'));
</script>
โ ๊ฒฐ๊ณผ
- ์ฒซ ํ๋ฉด
- ๋ต์ ๋ง์ท์ ๋
์ ๋ต์ด๋ผ๋ ๋ฌธ๊ตฌ๊ฐ ๋จ๊ณ ๋ค์ ๋ฌธ์ ๋ก ๋์ด๊ฐ๋ค.
- ๋ต์ ํ๋ ธ์ ๋
ํ๋ ธ๋ค๊ณ ์๋ฆฌ์ง๋ฅด๊ณ ๋ค์ ๋ฌธ์ ํ ๊ธฐํ๋ฅผ ์ค๋ค.
โ input ํ๊ทธ์ focus ์ฃผ๊ธฐ
: ref ์ฌ์ฉํ๊ธฐ
<input ref={(c) => {this.input = c;}} type="number" value={this.state.value}
onChange={this.onChange}/>
input ํ๊ทธ ์์ ref={(c) => {this.input = c;}} ๋ฅผ ๋ฃ์ด์ค๋ค.
// input focus๋ฅผ ์ํ ์ฝ๋
input;
// ์ฌ๊ธฐ์ {} ์์๋ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ๋ฃ์ ์ ์๋ค.
render() {
return (
<div>
<div>{this.state.first} ๊ณฑํ๊ธฐ {this.state.second} ๋?</div>
<form onSubmit={this.onSubmit}>
<input ref={(c) => {this.input = c;}} type="number" value={this.state.value}
onChange={this.onChange}/>
<button>์
๋ ฅ!</button>
</form>
<div>{this.state.answer} {this.state.result}</div>
</div>
);
}
}
return ๋ฐ๊นฅ์ input; ์ ์ด์ฃผ๊ณ
onSubmit = (e) => {
e.preventDefault();
if (parseInt(this.state.value) === this.state.first * this.state.second) {
// ์
๋ ฅํ ๊ฐ๊ณผ, ์ค์ ๊ฒฐ๊ณผ๊ฐ ๊ฐ๋ค๋ฉด ๊ฐ๋ค์ด ์ด๋ป๊ฒ ๋ฐ๋์ง
this.setState({
result: '์ ๋ต',
first: Math.ceil(Math.random() * 9),
second: Math.ceil(Math.random() * 9),
value: '',
answer: this.state.first * this.state.second,
});
this.input.focus();
} else {
this.setState({
result: '๋ก!!!!!!!!!!!!!!!!!!!',
value: '',
answer:'',
});
this.input.focus();
}
}
ํ๋ฉด์ state ๊ฐ ๋ฐ๋ ๋ ๋ง๋ค input ์ focus๊ฐ ๋๋๋ก
this.input.focus(); ๋ฅผ ์ ์ด์ค๋ค.
๊ทผ๋ฐ render ๋ ๋๋ฌด ์์ฃผ ์คํ๋๊ธฐ ๋๋ฌธ์ ref ๋ด์ฉ์ ๋ฐ๊นฅ์ผ๋ก ๋นผ์ฃผ๋ ๊ฒ์ด ์ข๋ค.
onRefInput = (c) => { this.input = c; };
<input ref={this.onRefInput} />
์ฐธ๊ณ : ์ธํ๋ฐ - ์น ๊ฒ์์ ๋ง๋ค๋ฉฐ ๋ฐฐ์ฐ๋ React (์กฐํ์)
'Front > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] npm error code ENOENT ํด๊ฒฐ ๋ฐฉ๋ฒ (3) | 2025.07.01 |
---|---|
[React] onClick์ ํ์ฉํ์ฌ ๋ฒํผ์ ๋๋ฅด๋ฉด ๊ฐ์ด ๋ณ๊ฒฝ๋๋ ์ฝ๋ ์์ฑํ๊ธฐ (4) | 2024.02.20 |
[React] ๋ฏธ๋ ๋ธ๋ก๊ทธ ๋ง๋ค๊ธฐ (0) | 2023.04.26 |
[React] styled-components (0) | 2023.04.25 |
[React] Font ์ ๊ด๋ จ๋ ์์ฑ (0) | 2023.04.25 |