简体   繁体   English

如何在React中同时更改多个孩子的状态

[英]How to change state of many childs in the same time in React

What is the sittuation? 情况是什么?

I Have a react component that render a table which users can select any cell they want. 我有一个react组件,可以呈现一个表,用户可以选择所需的任何单元格。

Users can select isolated cells or keep pressed the mouse and select many cells at once. 用户可以选择隔离的单元格或按住鼠标一次选择多个单元格。

This is working fine, you can see at: https://jsfiddle.net/gabirujoe/4drptu3f/ 一切正常,您可以在以下网址查看: https : //jsfiddle.net/gabirujoe/4drptu3f/

To do this, i have a component that render a table, and in the render function i have a for inside other for, to create the rows and the column of the table. 为此,我有一个渲染表的组件,在render函数中,我在其他for中有一个for,以创建表的行和列。

Inside the cells i have another react component with a state that marks if it is selected or not. 在单元格内部,我还有另一个反应组件,其状态会标记是否选中。 This Component receive the row and the column as properties, this is your id. 该组件将行和列作为属性接收,这是您的ID。

var Tabela = React.createClass({
getInitialState(){
    return {mouseDown:false};
},
handleMouseDown(event) {
    this.setState({mouseDown: true});
},
handleMouseUp(event) {
    this.setState({mouseDown: false});
},
render() {
     const list = [];
       for (let i = 0; i< 6; i++){
          const cubes = [];
           for (let j = 0; j< 6; j++){
              cubes.push(<td><Cube r={i} c={j} selected={false} 
                mouseDown={this.state.mouseDown} /> </td>);
          }
          list.push(<tr >{cubes}</tr>);
       }
      return (
             <div className="table" onMouseDown={this.handleMouseDown} 
                     onMouseUp={this.handleMouseUp} onMouseLeave={this.handleMouseUp}>
                  <table>{list}</table>
           </div>
       )
}});

var Cube = React.createClass({
getInitialState(props){
    return {selected: this.props.selected};
},
handleMouseDown(event) {
    this.setState({selected: !this.state.selected});
},
handleMouseEnter(event) {
    if(this.props.mouseDown)
        this.setState({selected: !this.state.selected});
},
render() {
    let cn = "cube"; 
     cn += ((this.state.selected) ? " selected" : "");
     return <div className={cn} onMouseDown={this.handleMouseDown}
     onMouseEnter={this.handleMouseEnter}> {"["+this.props.r+","+ this.props.c +"]"} </div>;
} });

What is the problem? 问题是什么?

I have a requisite that when user double click any cell, i have to change the state of selected to true of all the cells in a predefined axis of this cell. 我有一个要求,当用户双击任何一个单元格时,我必须在该单元格的预定义轴上将所有单元格的selected状态更改为true。 Can be horizontal axis, vertical axis or diagonal axis. 可以是水平轴,垂直轴或对角轴。

Anyone knows how i can find the cells of a predefined axis and change the state of all these cells in the same time? 谁知道我如何找到预定义轴的单元格并同时更改所有这些单元格的状态?

You should have the Tabela keep the selected state (as an array of array) and pass it to the Cube so the Cube has no state, only props. 您应该让Tabela保持选定状态(作为数组的数组)并将其传递给Cube,以便Cube没有状态,只有props。 That way, you can pass a double click handler from the Tabela to the Cube like : 这样,您可以将Tabela的双击处理程序传递给Cube,例如:

In Tabela:

handleDoubleClick: function (rowIndex, colIndex) {
   var selected = this.state.selected;
   var row = this.state.selected[rowIndex];
   // here i am inversing the row
   for (let r; r < row.length; r++) { 
       row[r] = !row[r]; // inverse state
   }
   this.setState({ selected: selected });
}

<Cube selected={ this.state.selected[i][j] }  onDoubleClick={ this.handleDoubleClick.bind(null, i, j) } 

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

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