Skip to content

Instantly share code, notes, and snippets.

@Joooohee
Created February 19, 2020 02:46
Show Gist options
  • Save Joooohee/599501690ba426d5e94c1466c254f912 to your computer and use it in GitHub Desktop.
Save Joooohee/599501690ba426d5e94c1466c254f912 to your computer and use it in GitHub Desktop.
react-gugudan-hooks
<html>
<head>
<meta charset="UTF-8" />
<title>구구단</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const GuGuDan = () => {
const [result, setResult] = React.useState("");
const [first, setFirst] = React.useState(Math.ceil(Math.random() * 9));
const [value, setValue] = React.useState("");
const [second, setSecond] = React.useState(
Math.ceil(Math.random() * 9)
);
const inputRef = React.useRef(null);
const onChangeInput = e => {
setValue(e.target.value);
};
const onSubmit = e => {
e.preventDefault();
if (parseInt(value) === first * second) {
setResult("정답" + value);
setFirst(Math.ceil(Math.random() * 9));
setSecond(Math.ceil(Math.random() * 9));
setValue("");
inputRef.current.focus();
} else {
setResult("땡");
setValue("");
}
};
return (
<React.Fragment>
<div>
{first} 곱하기{second} 는?
</div>
<form onSubmit={onSubmit}>
<input
ref={inputRef}
type="number"
value={value}
onChange={onChangeInput}
/>
<button>입력!</button>
</form>
<div>{result}</div>
</React.Fragment>
);
};
</script>
<script type="text/babel">
ReactDOM.render(<GuGuDan />, document.querySelector("#root"));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment