-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathreact.tsx
53 lines (47 loc) · 1.12 KB
/
react.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as React from 'react';
import {useState, useCallback, useRef, useEffect} from 'react';
const WordRelay = () => {
const [word, setWord] = useState('제로초');
const [value, setValue] = useState('');
const [result, setResult] = useState('');
const inputEl = useRef(null);
useEffect(() => {
console.log('useEffect');
}, []);
const onSubmitForm = useCallback((e) => {
e.preventDefault();
const input = inputEl.current;
if (word[word.length - 1] === value[0]) {
setResult('딩동댕');
setWord(value);
setValue('');
if (input) {
input.focus();
}
} else {
setResult('땡');
setValue('');
if (input) {
input.focus();
}
}
}, [word, value]);
const onChange = useCallback((e) => {
setValue(e.currentTarget.value)
}, []);
return (
<>
<div>{word}</div>
<form onSubmit={onSubmitForm}>
<input
ref={inputEl}
value={value}
onChange={onChange}
/>
<button>입력!</button>
</form>
<div>{result}</div>
</>
);
};
export default WordRelay;