【发布时间】:2017-05-10 22:47:58
【问题描述】:
我有两个组件。 家长:
export default class GameBoard extends React.Component{
constructor(props){
super(props);
this.state={
boardPattern:[
' ',' ',' ',
' ',' ',' ',
' ',' ',' '
],
turn: "",
winner: null
}
}
handleClick(i){
console.log(i);
this.state.board[i]=this.state.turn
this.setState({
board:this.state.board,
turn: this.state.turn==='./images/x.ico'?'./images/y.ico':'./images/x.ico'
})
}
render() {
return (
<div>
{this.state.board.map(function(value, i){
return <Square key={i} turn={this.state.turn} onChange={()=>this.handleClick(i)}/>
}.bind(this))}
</div>
)
}
}
孩子:
export default class Square extends React.Component{
handleClick = e => {
if ( typeof this.props.onTurnChange === 'function'){
this.props.onChange();
}
};
render(){
return <div className='Square' onClick={this.handleTurnChange}>
<img src={this.props.turn}></img>
</div>
}
}
我对 handleClick 功能有疑问。现在,当我单击每个方块时,每个方块中的状态“转”(添加源图像)都会发生变化。我怎样才能使我点击的 Square 中只出现图像?
【问题讨论】:
标签: reactjs tic-tac-toe