简体   繁体   English

React,我刚开始编码,我有一个问题

[英]React, I have just started coding and I have a question

I am making a tic-tac-toe game right now, and I want to change the className of a random MyClickable object, after another one Is clicked, but I couldnt find a way to do that.我现在正在制作井字游戏,我想更改随机 MyClickable object 的类名,然后单击另一个,但我找不到方法。 Thanks for your help.谢谢你的帮助。

 export default class Box extends Component { render() { return ( <div className="box"> <MyClickable id='1' /> <MyClickable id='2' /> <MyClickable id='3' /> <MyClickable id='4' /> <MyClickable id='5' /> <MyClickable id='6' /> <MyClickable id='7' /> <MyClickable id='8' /> <MyClickable id='9' /> </div> ); } } let botPlay = () => { clickability-- } let timeout = () => { setTimeout(botPlay, 1500) } let clickability = 0 class MyClickable extends Component { state = { className: 'a' }

tl;dr You need to store the game board as state and pass the value down to MyClickable as a prop. tl; dr您需要将游戏板存储为 state 并将值作为道具传递给MyClickable Then MyClickable just looks at it's value prop - eg null , X , or O - and it manages it's own class name, depending on the value prop.然后MyClickable只查看它的value prop - 例如nullXO - 它管理自己的 class 名称,具体取决于value prop。

Full solution:完整解决方案:

So start with your state - it should resemable a tic-tac-toe board and look something like this:所以从你的 state 开始 - 它应该类似于井字游戏板,看起来像这样:

[
  [null,null,null],
  [null,null,null],
  [null,null,null],
]

Now we also need to know who's turn it is, so let's add that to state as well (we'll start with X)现在我们还需要知道轮到谁了,所以让我们也将它添加到 state 中(我们将从 X 开始)

{
   playerTurn: 'X',
   board: [
     [null,null,null],
     [null,null,null],
     [null,null,null],
   ]
}

Great, now we know what our state is supposed to look like, let's make the component:太好了,现在我们知道我们的 state 应该是什么样子了,让我们制作组件:

expport class Board extends React.Component = {

   constructor() {
     this.state = {
       playerTurn: 'X',
       board: [
        [null,null,null],
        [null,null,null],
        [null,null,null],
      ]
     }
   }

   // when this is called, we update our board state and we change the player turn from X to O (or vice versa)
   handleClick = (row,col) => {
      // number 1 rule of React - don't mutate state (or props, or anything really)
      const nextBoard = [...this.state.board];

      // change the next turn to X, or O, depending on whose turn it is currently
      const nextTurn = this.state.playerTurn === 'X' ? 'O': 'X';


      // set the value of the board at row/col to X or O, depending on whose turn it is currently
      nextBoard[row][col] = this.state.playerTurn;

      // at this point you can determine if the game is over or not

      this.setState({playerTurn:nextTurn,board:nextBoard});
   }

   // helper - tells us if the game is over
   winner = () => {
     // return X if X has one, based on the state, or O is O has won, or null if nobody has won
     // leave this up to you to implement
   }

   render() {

     // if we have a winner, we can show who won!
     const winner = this.winner();
     if(winner) {
       return <div>{winner} won!</div>
     }

     // if we don't have a winner, show the game board
     return (
       <div className="box">
        {
          this.state.board.map((row,i) =>
            row.map((value,m) => (
              <MyClickable 
               // if this already has a value, pass undefined as `onClick`
               onClick={value ? undefined : () => this.handleClick(i,m)} 
               value={value}
             />
            ))
          )
         }
       </div>
     )
   }

}

Then you have MyClickable , something like this (we'll make a simple functional component because it doesn't have any state)然后你有MyClickable ,像这样(我们将制作一个简单的功能组件,因为它没有任何状态)

const MyClickable = ({onClick,value}) => {

  return (
    <div onClick={onClick}>
      {value}
    </div>
  )

}

If you want to change the class name of div rendered by the MyClickable component, all you need to do is look at value - it will either be null , X , or O :如果您想更改由MyClickable组件呈现的 div 的 class 名称,您需要做的就是查看value - 它可以是nullXO

const MyClickable = ({onClick,value}) => {

  return (
    <div className={value == null ? 'clickable' : 'already-clicked'} onClick={onClick}>
      {value}
    </div>
  )

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应原生。 我刚开始学习 React Native,我有一个关于 return 声明的问题 - React Native. I have just started learning react native and I have a question about return statement React Native:我对这个 React 有疑问 - React Native: I have a question with this React 开始使用node.js进行播放。 我有个问题 - Started Playing with node.js. I have a question 刚开始使用jwplayer,我有几个问题 - Just started working with jwplayer, I have a few questions 在 REACT 中设置 state 时,我有一个扩展运算符 es6 问题。 当前代码仅使用 1 个 object 覆盖数组 - I have a spread operator es6 question when setting state in REACT. The current code overwrites the array with just 1 object 我刚开始使用JavaScript。 我已将jquery库添加到我的html页面,但这不起作用 - i just started JavaScript. i have added jquery library to my html page but this is not working 我刚开始使用 firebase 并在获取数据时遇到 orderBy desc function 问题 - I just started with firebase and have problem with orderBy desc function when getting data 我刚刚开始使用 MEAN 堆栈。 我试图将我的表单提交数据存储到 mongo 数据库,但我无法这样做 - I have just started on MEAN stack. I was trying to store my form submit data to mongo database but I have been unable to do so 我刚开始学习 React 并陷入了这个错误 - I've just started learning React and got stuck at this error 我在Javascript中有关于Bubble Process的问题 - I have a question about Bubble Process in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM