Using es6 it can be done like this:

this.setState({ myArray: [...this.state.myArray, 'new value'] }) //simple value
this.setState({ myArray: [...this.state.myArray, ...[1,2,3] ] }) //another array

Spread syntax

Answer from Aliaksandr Sushkevich on Stack Overflow
🌐
React
react.dev › learn › updating-arrays-in-state
Updating Arrays in State – React
import { useState } from 'react'; ....id}>{artist.name}</li> ))} </ul> </> ); } ... Instead, create a new array which contains the existing items and a new item at the end....
Discussions

How can I push an object into an array of array in react trough reducer?
Array#push returns the new length of an array . You can add an object to an array with the array spread operator: return [ ...state, { question: action.payload.question, answer: action.payload.answer, }, ]; More on reddit.com
🌐 r/learnreactjs
4
3
March 13, 2023
Array.push() not working in React; I'm know you can't change state through push()
Hey there beautiful coders, I am trying to push some information to an array in the global scope (outside of any react Classes etc) by using the onClick event on some rendered JSX code. The information that I only want to push once the onClick event has been triggered is immediately being pushed ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
March 4, 2020
ReactJS Array.push function not working in setState
I'm making a primitive quiz app with 3 questions so far, all true or false. In my handleContinue method there is a call to push the users input from a radio form into the userAnswers array. It work... More on stackoverflow.com
🌐 stackoverflow.com
what is the correct way to push and pop an element to an array with useState?

I’d recommend functional updates since your state depends on previous state

setSquares(curr => […curr, “”])

More on reddit.com
🌐 r/reactjs
42
31
October 21, 2021
🌐
GitHub
github.com › yunusparvezkhan › Pushing_into_states_in_React
GitHub - yunusparvezkhan/Pushing_into_states_in_React: A documentation on How to push new data into React Array States and Object States · GitHub
You can notice the helper function named pushSong in the configuration file. That function is created to do some dataloading that will eventually give us the state updated with all the existing array elements and also the songname, got from the first arguement of that function, pushed into that array state.
Author: yunusparvezkhan
🌐
Bobby Hadz
bobbyhadz.com › blog › react-push-to-state-array
How to push an Element into a state Array in React | bobbyhadz
The spread syntax (...) will unpack the existing elements of the state array into a new array where we can add other elements. ... Copied!import {useState} from 'react'; export default function App() { const [names, setNames] = useState(['Alice', 'Bob']); const handleClick = () => { // 👇️ Push to the end of the state array setNames(current => [...current, 'Carl']); // 👇️ Spread an array into the state array // setNames(current => [...current, ...['Carl', 'Delilah']]); // 👇️ Push to the beginning of the state array // setNames(current => ['Zoey', ...current]); }; return ( <div> <div> <button onClick={handleClick}> Push to state array </button> </div> {names.map((element, index) => { return ( <div key={index}> <h2>{element}</h2> </div> ); })} </div> ); }
🌐
Reddit
reddit.com › r/learnreactjs › how can i push an object into an array of array in react trough reducer?
r/learnreactjs on Reddit: How can I push an object into an array of array in react trough reducer?
March 13, 2023 -

I am super new to react so I'm completely confused as to what's going on.

I am trying to add "question" and "answer" as an object to a certain array based on the option value selected.
Option value works, as well as payload from inputs, but I can't seem to get the logic of the return with state here:

return [
          ...state,
          htmlArray.push({
            question: action.payload.question,
            answer: action.payload.answer,
          }),
        ];

it just adds number 1 to the state, as another element in state array. I know I should separate my components, but I'm new to all of this, so it's easier for now to think of it in just one file.

Here's the code with removed other cases for transparency :

import React, { useReducer, useState } from "react";
import { v4 as uuidv4 } from "uuid";

const Form = () => {
    
  const [question, setQuestion] = useState("");
  const [answer, setAnswer] = useState("");
  const [option, setOption] = useState("html");

  let htmlArray = [];
  let cssArray = [];
  let jsArray = [];
  let reactArray = [];
  let theoryArray = [];

  const questionReducer = (state, action) => {
    switch (action.type) {
      case "html":
        return [
          ...state,
          htmlArray.push({
            question: action.payload.question,
            answer: action.payload.answer,
          }),
        ];
      default:
        break;
    }
  };

  const [state, dispatch] = useReducer(questionReducer, [
    htmlArray,
    cssArray,
    jsArray,
    reactArray,
    theoryArray,
  ]);
  console.log(state);

  const handleSubmit = (e) => {
    e.preventDefault();
    dispatch({
      type: option,
      payload: {
        question: question,
        answer: answer,
      },
    });
    setQuestion("");
    setAnswer("");
  };

  return (
    <form onSubmit={handleSubmit}>
      <input
        placeholder="question"
        onChange={(e) => setQuestion(e.target.value)}
        value={question}
      />
      <input
        placeholder="answer"
        onChange={(e) => setAnswer(e.target.value)}
        value={answer}
      />
      <select value={option} onChange={(e) => setOption(e.target.value)}>
        <option value="html">HTML</option>
        <option value="css">CSS</option>
        <option value="js">JS</option>
        <option value="react">React</option>
        <option value="theory">CS</option>
      </select>
      <button type="submit" value="Add question">
        Enter Question
      </button>
      <div>{question}</div>
      <div>{answer}</div>
    </form>
  );
};
export default Form;
🌐
freeCodeCamp
forum.freecodecamp.org › t › array-push-not-working-in-react-im-know-you-cant-change-state-through-push › 355876
Array.push() not working in React; I'm know you can't change state through push()
March 4, 2020 - Hey there beautiful coders, I am trying to push some information to an array in the global scope (outside of any react Classes etc) by using the onClick event on some rendered JSX code. The information that I only want to push once the onClick event has been triggered is immediately being pushed ...
🌐
DhiWise
dhiwise.com › post › building-dynamic-lists-how-to-use-react-usestate-array-push
How to Efficiently Use React Usestate Array Push
August 20, 2024 - JavaScript arrays are mutable, which means they can be modified directly. However, when an array is stored in React state, you should treat it as immutable. To add a new element to an array state variable, you should not use the array object's push method directly on the state variable.
Find elsewhere
🌐
Quora
quora.com › How-do-you-push-an-object-into-an-array-in-React
How to push an object into an array in React - Quora
Answer (1 of 2): Hey, I think that the question isn’t very clear. But, I’ll list down the 2 possible questions that I assume you have a doubt in. 1. How do you push an object into an array in Javascript? 2. 1. This is pretty simple. You initialize an array first.
🌐
Devsheet
devsheet.com › code-snippet › reactadd-or-push-values-to-array-defined-in-state
[React]Add or push values to array defined in state - Devsheet
reactjs · var newArr = ... arr: [{"name": "value"}, ...this.state.arr] }); You can use dot operators to insert new values to the array if you are using ES6 or you can also concatenate an array to the new values using ...
🌐
Medium
wesguirra.medium.com › a-guide-for-manipulate-array-state-in-a-react-application-techniques-for-mutating-arrays-2db236d5b21c
A guide for manipulate Array state in a React Application: Techniques for mutating Arrays | by Wes Guirra | Medium
January 3, 2023 - To add new item, we have declared a simple function responsible only to add new tasks to our task list, there we just receive the task from onSubmit event from NewTaskInput, then we create an Array copy of current tasks, we use push method and we use the length of the array as id to create new item, but it can leverage to error saying that items need to be unique when we delete an item and create one just after it, so a good idea is to use uuid library to do that.
🌐
IQCode
iqcode.com › code › javascript › react-state-array-push
react state array push Code Example
February 27, 2022 - this.setState(prevState =&gt; ({ myArray: [...prevState.myArray, &quot;new value&quot;] }))
🌐
YouTube
youtube.com › caleb curry
How to Push to State Array - React Tutorial 13 - YouTube
Start your software dev career - https://calcur.tech/dev-fundamentals ⚛️ FREE React Course (download & bonus content) - https://calcur.tech/free-react-course
Published: September 2, 2022
Views: 15K
🌐
CodePen
codepen.io › a810524z › pen › XKoQoB
React push array
class ChildA extends React.Component { constructor(props) { super(props); this.state = { value: ['a', 'b' , 'c', 'd', 'e'], textvalue : "", test:"" } this.handleAddTodoItem = this.handleAddTodoItem.bind(this) this.handleChange = this.handleChange.bind(this) this.handledelTodoItem = this.handledelTodoItem.bind(this) } handleChange(e) { this.setState({ textvalue:e.target.value }) } handleAddTodoItem() { this.state.value.push(this.state.textvalue) this.setState( this.state ) this.state console.log(this.state.value) } handledelTodoItem(v){ for(var i = 0; i < this.state.value.length; i++){ if(this.
🌐
Medium
medium.com › @susarlamallikarjun › understanding-push-vs-array-newvalue-in-javascript-and-react-dafb6039e686
Understanding .push() vs [...array, newValue] in JavaScript and React | by Mallikarjun | Medium
March 13, 2025 - Use .push() when modifying temporary variables. Use [...array, newValue] for React state updates to trigger re-renders. For React state → [...array, newValue] (immutability matters). For performance-critical operations outside state → .push() (mutate safely).
🌐
The Web Dev
thewebdev.info › home › how to push or append an element to a state array with react hooks?
How to Push or Append an Element to a State Array with React Hooks? - The Web Dev
March 13, 2021 - To update a React component state array with a new item at the end of it, we can pass in a callback to the state setter function that takes the old array value and return the new array value.
🌐
DevPress
devpress.csdn.net › react › 62eb7bd920df032da732b4d8.html
How to Use Push Method In React Hooks?_reactnative_weixin_0010034-React
August 4, 2022 - The push () method is the process of adding one or more numbers of elements at the end of the array and returning with a new length of the array. Normally, the push method helps to add the value to t weixin_0010034 React
🌐
The Web Dev
thewebdev.info › home › how to push an element inside an array state with react hook?
How to Push an Element Inside an Array State with React Hook? - The Web Dev
March 2, 2021 - In this article, we’ll look at how to push an item into an array if we have an array state with React hooks.