인프런 커뮤니티 질문&답변

슈퍼루키님의 프로필 이미지

작성한 질문수

웹 게임을 만들며 배우는 React

클래스 메서드

결과값 오류......

20.03.14 22:08 작성

·

177

0

안녕하세요 다름이 아니라 강좌를 보고 저 혼자 코딩을 해봤는데 오류가 뜨네요 ㅠㅠ

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin 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>
<title>React Web 구구단 구현</title>
</head>
<body>
    <div id="root"></div> <!-- 결과 : <div id="root"><button>Like</button></div> -->
    <script type="text/babel">
        class GuGuDan extends React.Component {
            constructor(props) {
                super(props);
                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: '',
                           });
                       }

            render() {
                return (
                <div>
                   <div>{this.state.first} 곱하기 {this.state.second}는??</div>
                    <form onSubmit = {this.onSubmit}
                        <input type="number" value={this.state.value} onChange={(e=> this.setState({ value: e.target.value })} />
                        <button>입력!!</button>
                   </form>
                   <div>{this.state.result}</div>
                </div>
                );
            }
        }
        // <input type="number" value={this.state.value} onChange={(e) => this.setState({ value: e.target.value })} />
        // input.onchange = (e) => { console.log(e.target.value) }
        // input.onchange = function (e) { console.log(e.target.value) }
    </script>
    <script type="text/babel">
        ReactDOM.render(<GuGuDan />, document.querySelector('#root'));
    </script>
</body>
</html>

답변 1

0

당근같은사람님의 프로필 이미지

2020. 05. 06. 22:33

<form onSubmit = {this.onSubmit} 하시고 >를 안닫으셔서 문법오류가 났습니다.