title: Update data from array with multiple objects using Axios PUT request
body: <p>I need to update data in an array that has multiple objects where a user will input a new balance that will update the old balance state. The array consists of a company name with an array called details, and that array holds objects containing employee information(name, balance, notes), for this question I am just using notes to simplify things. I am using Axios PUT to access the id of the nested object, I get the id from a Link that is passed via useParams hook.</p>
  <p>My issue is in the Axios PUT request. Before I had a schema that was just a data object (no arrays were in it) and the PUT req was working fine. Then I needed to change the schema to an array with multiple objects and now I cannot seem to update the data. I am able to target the data through the console log but when I take that code from the console and apply it, the state still doesn't change. Even in Postman, the only way for me to successfully update is to get the Shema from a GET request and paste that schema in the PUT request and change some data in it, then I hit send and it updates, but to get it to update again I need to hit send twice (this shouldn't be, no? ).</p>
  <p>I am able to access the data and render it in other components by mapping it twice as shown below:
  <code>setBalance(res.data.data.details.map((r) =&gt; r.balance));</code></p>
  <p><strong>My question: How can I edit the below code to update the state correctly?</strong>
  <code>setNotes([...details, res.data.data.details.map((r) =&gt; r.notes )]);</code></p>
  <p>However, I am really struggling with how to do this in the Axios PUT request.</p>
  <p>Here is my code:</p>
  <pre><code>import React, { useState } from &quot;react&quot;;
  import { useHistory } from &quot;react-router-dom&quot;;
  import { useParams } from &quot;react-router-dom&quot;;
  import axios from &quot;axios&quot;;
  
  const AddForm = () =&gt; {
    const [newBalance, setNewBalance] = useState(&quot;&quot;);
    const [details, setDetails] = useState([]);
    const [notes, setNotes] = useState(&quot;&quot;);
    const [total, setTotal] = useState(&quot;&quot;);
    const { id } = useParams();
    const history = useHistory();
  
    //update balance
    const updateBal = () =&gt; {
    // function to calculate balance
    };
  
    const updateBalHandler = (e) =&gt; {
      e.preventDefault();
      axios({
        method: &quot;PUT&quot;,
        url: `http://localhost:5000/update-snapshot-test/${id}`,
        data: { 
                balance: total
                notes: notes 
              },
      }).then((res) =&gt; {
        history.push(`/success/` + id);
        setNotes([...details, res.data.data.details.map((r) =&gt; r.notes )]); //this code isolates the notes state but does not update it
      });
    };
  
    return (
      &lt;form
        action=&quot;/update-snapshot/:id&quot;
        method=&quot;post&quot;
        onSubmit={updateBalHandler}
      &gt;
          &lt;Input
            setInputValue={setNewBalance}
            inputValue={newBalance}
            inputType={&quot;number&quot;}
          /&gt;
  
          &lt;Input
            setInputValue={setTotal}
            inputValue={total}
            inputType={&quot;number&quot;}
          /&gt;
  
          &lt;TextArea
           setInputValue={setNotes}
           inputValue={notes}
           inputType={&quot;text&quot;}
          /&gt;
  
          &lt;Button onClick={() =&gt; { updateBal(); }} &gt;
          Update
         &lt;/Button&gt;
  
          &lt;Button type=&quot;submit&quot;&gt;
           Save
          &lt;/Button&gt;
  
      &lt;/form&gt;
    );
  };
  export default AddForm;
  </code></pre>
  <p>Here is my data structure from Mongo DB</p>
  <pre><code>{
      &quot;message&quot;: &quot;Post found&quot;,
      &quot;data&quot;: {
          &quot;company&quot;: &quot;Riteaid&quot;,
          &quot;_id&quot;: &quot;1&quot;,
          &quot;details&quot;: [
              {
                  &quot;employee&quot;: &quot;jane doe&quot;,
                  &quot;balance&quot;: &quot;3&quot;,
                  &quot;notes&quot;: &quot;some notes&quot;,
                  &quot;_id&quot;: &quot;2&quot;
              },
              {
                  &quot;employee&quot;: &quot;john doe&quot;,
                  &quot;balance&quot;: &quot;1&quot;,
                  &quot;notes&quot;: &quot;some more notes&quot;,
                  &quot;_id&quot;: &quot;3&quot;
              }
          ],
      }
  }
  </code></pre>