Add array also to the data object in React.js

Viewed 47

I want to concatenate ALLActivity with Data but I don't know how to achieve that.

const [ALLActivity, setAllActivity] = useState([]);

I have different input fields that I get using onchange. They are then stored in a useState ALLActivity.

const getActivity = (e) => {
   setAllActivity({ ...ALLActivity, [e.target.name]: e.target.value });
 }; 

I want to combine the fields if ALLActivity and id

const submitnewActivity = (e) => {
    const Data = {
       ALLActivity,  //What should I do to fix here
       { id: e },
               }

 };
axios
    .patch("edit-activity/",ALLActivity,  //I want pass id along like this

      {
        headers: headers,
      }
    ) 
1 Answers

Try something like this

const submitnewActivity = (e) => {
    const Data = {
       ...ALLActivity,  
        id: e 
    }
 };

This will spread the ALLActivity object and then the id will be added to the data object.

Related