使用setState方法以嵌套对象格式更新状态数据

时间:2018-06-04 08:14:05

标签: reactjs jsx

class TreeStructure extends Component {
  constructor(props) {
    super(props);
    this.state = {
        componentData: {},
        modalIsOpen: false,
    }
    this.cleanObject = this.center code hereleanObject.bind(this);
    this.openModal = this.openModal.bind(this);
    this.closeModal = this.closeModal.bind(this);
    this.update = this.update.bind(this);
  }

  componentDidMount() {
    this.setState({
      componentData: this.props.componentData
    })
  }

  componentWillReceiveProps(nextProps){
    this.setState({
      componentData: nextProps.componentData
    })
  }

  openModal(){
    this.setState({
      modalIsOpen: true,
    })
  }

  closeModal(){
    this.setState({
      modalIsOpen: false,
    })
  }

  update(key, subKey, k, values){
    // this.setState({
    //   modalIsOpen: true,
    // })
    console.log("key: " + key)
  }

  cleanObject(obj1){
    const obj = Object.assign({}, obj1);
    delete obj.__v;
    delete obj._id;
    delete obj.name;
    return obj;
  }

  render() {
    return(
        <div>
          <Modal
            isOpen={this.state.modalIsOpen}
            onRequestClose={this.closeModal}
            contentLabel="Add Expense"
            className="Modal">

            <form>
              <input type="text"/>: 
              <input type="text"/>
            </form>

          </Modal>

          {Object.keys(this.state.componentData).map((key, i) => {
            return (
              <TreeView 
                key={i}
                nodeLabel={key}
                // defaultCollapsed={true}
              > <button>Add Row</button>
                {Object.keys(this.state.componentData[key]).map((subKey, j) => {
                  return (
                    <TreeView
                      key={j}
                      nodeLabel={subKey}
                      // defaultCollapsed={true}
                    > <button>Add Row</button>
                      {this.state.componentData[key][subKey].map((superSubComponent, k) => {
                        <div>{superSubComponent = this.cleanObject(superSubComponent)}</div>
                        return(
                          <TreeView
                            key={k}
                            nodeLabel={k}
                            // defaultCollapsed={true}
                          > <button>Add Key and Value</button>
                            {Object.keys(superSubComponent).map((values, l) => {
                              return (
                                // <div key={l}>{values}: {superSubComponent[values]}<button onClick={this.update}>Edit</button><button>Delete</button></div>
                                <div key={l}>
                                  <div className="key">{values}: </div>
                                  <div className="value">{superSubComponent[values]}</div>
                                  <button onClick={this.update(key, subKey, k, values)}>Edit</button><button>Delete</button>
                                </div>
                              )
                            })}
                          </TreeView>
                        )
                      })}
                    </TreeView>
                  )
                })}
              </TreeView>
            )
          })}
        </div>
    );
  }
}

我想用componentData更新this.update(状态中)。有人可以给我一些关于如何做的建议吗?我尝试在update()中更新状态,但我得到maximum update depth exceeded错误。我的主要目标是更新componentData组件中的TreeStructure,然后使用它来更新mongo数据库。由于架构没有修复,我认为替换数据库中的整个文档可能是最好的选择。所以我需要compoentData作为文档。

1 个答案:

答案 0 :(得分:2)

问题在于:

onClick={this.update(key, subKey, k, values)}

你在渲染时调用它,调用会更新状态,触发重新渲染,再次执行该调用,依此类推。改为给onClick一个函数:

onClick={() => this.update(key, subKey, k, values)}