简体   繁体   English

如何更改按钮不可靠的 React.js 的值

[英]How to change the value of buttons undependable React.js

I am trying to build a tic-tac-toe game using class to practice passing the props to child components and lifting up the state of component that's why you can see my creating 4 components.我正在尝试使用 class 构建井字游戏,以练习将道具传递给子组件并提升组件的 state,这就是为什么您可以看到我创建 4 个组件的原因。

The problem I need help with is so that when I click one of the buttons all buttons change their value at the same time.我需要帮助的问题是,当我单击其中一个按钮时,所有按钮都会同时更改它们的值。

I need to make each button display its own value separately.我需要让每个按钮分别显示自己的值。 I declared a function and gave it functionality to change the state from null to X or O.我声明了一个 function 并赋予它将 state 从 null 更改为 X 或 O 的功能。

// App component 
class App extends React.Component {
  constructor(props) {
    super(props);
 
  }
  render() {
    return (
      <>
        <Board />
      </>
    );
  }
}

// Board component 
class Board extends React.Component{
  constructor(props){
    super(props);
    this.state = {txt: 'X'};
    this.change = this.change.bind(this);
  }
  change(){
    const updt = (this.state.txt === 'X' || this.state.txt === '0') ? 'O' : 'X';
    this.setState({txt: updt}); 
  }
  render() {
    return(
       <>
        <div>
          <Row key={'1'} value={this.state.txt} change={this.change}/>
          <Row key={'2'} value={this.state.txt} change={this.change}/>
          <Row key={'3'} value={this.state.txt} change={this.change}/>
        </div>
      </>
    )
  }
}

// Box component 
function Box(props){
  return (
    <>
      <button  className='class1' onClick={props.change} >{props.value}</button>
    </>
  );
}

// Row component 
function Row(props){
    return (
      <>
        <div id='myId'>
          <Box change={props.change} value={props.value}/>
          <Box change={props.change} value={props.value}/>
          <Box change={props.change} value={props.value}/>
        </div>
      </>
    )
}


ReactDOM.render(<App/>, document.querySelector('#root'));

all of your button change text because you literally passing them the same value, instead you need to create an array of rows each row contains an array of buttons, and then you map over these array like I'm down down below, that's how things are meant to be don in react.你所有的按钮都会改变文本,因为你实际上传递了相同的值,而不是你需要创建一个行数组,每行包含一个按钮数组,然后你在这些数组上 map 就像我在下面一样,事情就是这样旨在做出反应。 I also added a user turn functionality.我还添加了用户转向功能。

// App component 
class App extends React.Component {
  constructor(props) {
    super(props);
 
  }
  render() {
    return (
      <>
        <Board />
      </>
    );
  }
}

// Board component 
class Board extends React.Component{
  constructor(props){
    super(props);
    this.state = {
turn:"X",

rows:[
//ROW 1 
         [
            {
            id:"btn1",
            text:"0"
            },
            {
            id:"btn2",
            text:"0"
            },
            {
            id:"btn3",
            text:"0"
            },
         ],
//ROW 2 
         [
            {
            id:"btn1",
           text:"0"
            },
            {
            id:"btn2",
            text:"0"
            },
            {
            id:"btn3",
            text:"0"
            },
         ],
//ROW 3 
         [
            {
            id:"btn1",
             text:"0"
            },
            {
            id:"btn2",
             text:"0"
            },
            {
            id:"btn3",
            text:"0"
            },
         ],
    ]};
    this.change = this.change.bind(this);
  }

  change(rowIndex,btnId,btnLastValue){
   
    const updt = (btnLastValue === 'X' || btnLastValue === '0') ? 'O' : 'X';
    let tempState= [...this.state.rows]
    const targetBtn= tempState[rowIndex].filter(btn=>btn.id==btnId)[0]
    const targetBtnIndex= tempState[rowIndex].indexOf(targetBtn)
    
    let updateText = this.state.turn=="X" ? "X": "O"
    const tempState[rowIndex][targetBtnIndex].text= updateText 
    
    this.setState({...this.state,rows: tempState, trun:updateText }); 
  }

  render() {
    return(
       <>
        <div>
          {
           this.state.rows.map((row,index)=><Row 
           key={index} 
           rowIndex={index} 
           row={row} 
           change={this.change}
           />
           )
          }
        </div>
      </>
    )
  }
}

// Box component 
function Box(props){
 const {value,rowIndex,change}=props
  return (
    <>
      <button  className='class1' onClick={e=>change(rowIndex,value.id,value.text)} >{props.value}</button>
    </>
  );
}

// Row component 
function Row(props){
    const {row,rowIndex,change}=props

    return (
      <>
        <div id='myId'>
           {
            rows.map(btn =><Box 
            change={change} 
            rowIndex={rowIndex} 
            value={btn}
            />)
            }  
        </div>
      </>
    )
}


ReactDOM.render(<App/>, document.querySelector('#root'));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM