강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của gilsun08211642
gilsun08211642

câu hỏi đã được viết

Học React bằng cách tạo trò chơi trên web

giới thiệu

focus

Viết

·

236

0

중간에 포커스 해주는 부분을 넣지 않아도 그냥 포커스가 되는데요. 이게 다른 이유 ( 제 브라우저, 아님 익스텐션 때문에) 때문일수도 있을까요? 

<html>
  <head>
    <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>
  </head>
  <body>
    <div id="root"></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((prevState) => {
              return {
                result: '정답:' + prevState.value,
                first: Math.ceil(Math.random() * 9),
                second: Math.ceil(Math.random() * 9),
                value: '',
              };
            });
            // this.hello.focus();
          } else {
            this.setState({
              result: '땡',
              value: '',
            });
            // this.hello.focus();
          }
        };
        onChange = (e) => {
          this.setState({ value: e.target.value });
        };
        hello;
        render() {
          return (
            <React.Fragment>
              <div>
                {this.state.first}곱하기{this.state.second}는?
              </div>
              <form onSubmit={this.onSubmit}>
                {' '}
                <input
                  //   ref={(c) => {
                  //     this.hello = c;
                  //   }}
                  type="number"
                  value={this.state.value}
                  onChange={this.onChange}
                />
                <button>입력!</button>
              </form>
              <div>{this.state.result}</div>
            </React.Fragment>
          );
        }
      }
    </script>
    <script type="text/babel">
      ReactDOM.render(
        <div>
          <GuGuDan />
          <GuGuDan />
          <GuGuDan />
          <GuGuDan />
        </div>,
        document.querySelector('#root')
      );
    </script>
  </body>
</html>
react

Câu trả lời 2

0

gilsun yu님의 프로필 이미지
gilsun yu
Người đặt câu hỏi

써주신 답글보고 한참 생각해 보니 제가 focus를 전혀 이해하지 못했네요.
https://www.youtube.com/watch?v=TP-v0pW1c0E
버튼 ..즉 입력버튼을 누루고 나서 커서가 알아서 박스안에 들어가게 하는 이벤트였네요 !  
감사합니다. 

0

zerocho님의 프로필 이미지
zerocho
Người chia sẻ kiến thức

엔터 치신거면 인풋 바깥으로 빠져나온 적이 없어서 계속 남아있는게 아닐까요?

Hình ảnh hồ sơ của gilsun08211642
gilsun08211642

câu hỏi đã được viết

Đặt câu hỏi