Reactjs 通过Axios&;附加到阵列;反应

Reactjs 通过Axios&;附加到阵列;反应,reactjs,axios,Reactjs,Axios,我想通过axios将一个对象附加到JSON数组中,并做出反应。 我尝试过这样做,但它只是覆盖了整个数组,所以只有新数据存在 axios.put(url, { KPIS: [ values.form ] } ) .then(res => { console.log(res); console.log(res.data); }) 这在

我想通过axios将一个对象附加到JSON数组中,并做出反应。 我尝试过这样做,但它只是覆盖了整个数组,所以只有新数据存在

axios.put(url, {
            KPIS: [
              values.form
            ]
          } )
      .then(res => {
        console.log(res);
        console.log(res.data); 
  
      })

这在很大程度上取决于API如何编程来处理put。通常,它只会将值替换为您发送的任何内容

但是,您始终可以自己将数据附加到数组中,然后对其调用put。您可以这样做,将表单值添加到当前数组中。您必须首先使用get请求获取当前值,否则API将无法工作

axios.put(url, {
            KPIS: [
              ...currentArray, values.form
            ]
          } )
      .then(res => {
        console.log(res);
        console.log(res.data); 
  
      })