React 튜토리얼에서 그들은 tic-tac-toe 게임을 만든다. 이 코드를 얻을 때까지는 쉽게 이해할 수있었습니다.어디에서 왔는가?
handleClick(i) {
const squares = this.state.squares.slice();
squares[i] = 'X';
this.setState({squares : squares});
}
이 코드는 click 이벤트 처리기입니다. 하지만 코드에서이 변수가 어디서 왔는지는 알지 못했습니다. React가 클릭하는 정확한 버튼을 어떻게 알 수 있습니까? 튜토리얼을 읽는 사람이 설명해 주시겠습니까? 원본을 찾을 때까지
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
class Square extends Component {
render(){
return (
<button
className="square"
onClick={() => this.props.onActivation()}>
{this.props.value}
</button>
);
}
}
class Board extends Component {
constructor(){
super();
this.state = {
squares: Array(9).fill(null),
}
}
handleClick(i) {
const squares = this.state.squares.slice();
squares[i] = 'X';
this.setState({squares : squares});
}
renderSquare(i) {
return (
<Square
value={this.state.squares[i]}
onActivation={()=>this.handleClick(i)}
/>
);
}
render(){
const status = "Next Player: X";
return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div>
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div>
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
)
}
}
class Game extends Component {
render() {
return (
<div className="game">
<div className="game-board">
<Board/>
</div>
<div className="game-info">
<div>{/* status */}</div>
<ol>{/* TODO */}</ol>
</div>
</div>
)
}
}
ReactDOM.render(<Game/>, document.getElementById('root'));
에서 사용할 수있는 이유
는, 즉'i' 인수입니다입니다 그 방법에 ... – Li357
알아,하지만 값이 handleClick (i)에서 왔어? 클릭 할 때만 실행됩니다. 맞습니까? –
무슨 소리 야? 값이 메서드 – Li357