在状态下将对象添加到数组

时间:2019-12-21 19:28:39

标签: stack mern

如何将对象推入状态,而又不丢失状态中的先前数据。 然后,我想使用Mongoose将对象数组发送到我的MongoDB。 实际上,我一次只能保存一个对象。

state{
    detalleTemp:[]
}

 onSubmitDetalle = e =>{
    e.preventDefault();
    const detalleTemp ={
      nombreDanza : this.state.danzaSelected,
      generoTraje: this.state.generoSelected,
      tallaTraje: this.state.tallaSelected,
      cantidadTraje: this.state.cantidad
    }
    this.setState({
      detalleTemp+= //this part how can i do it?
    })
    console.log(detalleTemp)
    // this.getDetalles();
  }

这是我的表格

<div className="card card-body">
              <h3>Detalles</h3>
                <form onSubmit={this.onSubmitDetalle}>
                  <div className="form-group">
                    <select className="form-control"
                      name="danzaSelected"
                      onChange={this.onChangeInput}>
                      {this.state.danzas.map(danza =>(
                        <option value={danza} key={danza}>
                          {danza}
                        </option>
                      ))}
                    </select>
                  </div>
                  <div className="form-group">
                    <select name="generoSelected"
                      className="form-control"
                      onChange={this.onChangeInput}>
                      {this.state.generos.map( genero =>(
                        <option value={genero} key={genero}>
                          {genero}
                        </option>
                      ))}
                    </select>
                  </div>
                  <div className="form-group">
                    <select name="tallaSelected"
                      className="form-control"
                      onChange={this.onChangeInput}>
                      {this.state.tallas.map(talla =>(
                        <option value={talla} key={talla}>
                          {talla}
                        </option>
                      ))}
                    </select>
                  </div>
                  <div className="form-group">
                    <input type="text"
                      className="form-control"
                      name="cantidad"
                      placeholder="Cantidad"
                      onChange={this.onChangeInput}/>
                  </div>
                  <button type="submit" className="btn btn-success btn-block">
                    Añadir
                  </button>
                </form>
                <div className="form-group"></div>

            </div>
  

[{“ _ id”:“ 5dfe71c907c0596ca8938e59”,“ nombreCliente”:“ Eojeda”,“ apellidosCliente”:“ sadsa”,“ dniCliente”:“ s4665465”,“ domicilioCliente”:“”,“ fechaInicio”:“ 2019 -12-21T19:25:23.611Z“,” fechaDevolucion“:” 2019-12-22T19:25:23.000Z“,” precio“:” 20“,” detalle“:{” nombreDanza“:”Cápac   Chuncho“,” generoTraje“:” Varon“,” tallaTraje“:” XS“,” cantidadTraje“:” 2“},” createdAt“:” 2019-12-21T19:26:01.678Z“,” updatedAt“:” 2019-12-21T19:26:01.678Z“,” __ v“:0}]

0 个答案:

没有答案