How to add object in an array using useState

import React, { useState } from 'react';
 
function App() {
 
  const [items, setItems] = useState([]);
 
  // handle click event of the button to add item
  const addMoreItem = () => {
    setItems(prevItems => [...prevItems, {
      id: prevItems.length,
      value: getRandomNumber()
    }]);
  }
 
  // generate 6 digit random number
  const getRandomNumber = () => {
    return Math.random().toString().substring(2, 8);
  }
 
  return (
    <div classname="App">
      <h3>useState with an array in React Hooks - <a href="https://www.cluemediator.com">Clue Mediator</a></h3>
      <br>
      <button onclick="{addMoreItem}">Add More</button>
      <br><br>
      <label>Output:</label>
      <pre>{JSON.stringify(items, null, 2)}</pre>
    </div>
  );
}
 
export default App;

4
4

                                    setMyArray(oldArray =&gt; [...oldArray, newElement]);

4 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
react js hook push object to array state react js setState add object to array usestate adding to array react usestate hook adding to array add value to array with usestate how to use usestate to add array of objects adding object in array react usestate push new object array raect usestate how to add objects to array in usestate how tpo add new objects in array using setState add array of objects javascript hooks useState and push obejct to array with in object in anrray usestate append arrays how to add values in array using useState how to push object in useState array in react-nativer how to push an object in array usestate setstate push array of objects how to add object to array of objects in useState how to add object to array of objects in setState react array of object append value hooks push object in array react in usestate push obj in useState array react add object to array useState usestate array adding how to add new object in array using setstate in functional component how to add new object in arrat using setstate react adding to array in usestate react useState add array add objects to array in use state hook add element into an array in react using usestate setState array objects react usestate add element add array to an existing useState array usestate add new object to array Add Data into an Array in a State Object using usestate usestate add array of objects how to add item into usestate aray add item to array in object react usestate add object to array in react hooks add new object to setstate array react use setstate hook to add object to array how to append value in array react usestate how to add data into usestate array how to push object in array in usestate in js usestate add array to usestate array usestate add array to array how to append usestate array how to push object element into array using hooks in react native how to add object to usestate array in reactjs setstate how to add in create array of objects in react js hooks react js usestate adding more item to array of objects react js usestate add array element reactjs usestate array of objects add react add item to array in state hooks how to add object to array using useState how to add elements in array by usestate add object to array hooks add object to usestate array how to add to usestate array in react react usestate add array to array add object in useState array add an object to array in set react hooks adding an obj to an array in useState hook add array values in usestate hook how to add object in array with useState react how to add an element in array usestate how to push object to an array in usestate usestate array push objects adding an object to existing array with react hooks expo add array of object to usestate add an object to array in react hooks useState how to insert object in array javascript in react usestate react hooks add object to array react append array of objects usestate add item in array usestate hooks react usestate add data to array how to push object into array react hooks react usestate add to array corect way adding item on useState array useState array adding object how to add an object to an array in react hooks proper way to add item to array in react hooks usestate react set array and add item add items to state array react usestate usestate push object to array usestate to add something to array add object to array usestate add object inside array react hooks add object to array reactjs hooks add an object to an existing array using hooks reactjs add an object to an array using hooks reactjs adding array to a usestate add object to state array react hooks usestate array add list of objects react add to array usestate react usestate setstate add to array add array in usestate react add item to array usestate react usestate add new item to array add object to state array how to add to array of object in react using hooks add object in array usestate add data to array inside an object using usestate how to add objects to useState array usestate add object to array how to add object to array in react useState usestate how to add an object to array how to use setState to add new object in array push object into array state hook react usestate state array add item add obj to array javascript usestate functional add object to arrray react state react usestate add to array add an object to an array react use state how to use useState function to push object into an array in object how to add object to an array in an object using useState how do i insert into an array inside a object using usestate object to array push in react usestate add objects to state array add objects to array state push objects into array in usestate hooks add object to array use state useState set add array add object to array with hooks react hooks usestate objects push into arrays how to push value in object inside array of objects usestate how to push value in array of objects usestate add object to array setstate how to push value in object of array in usestate how to push value in object of object of array in usestate how to add data inside object of array in usestate setstate add object to array how to add an object to array in react hooks push object into state array react hooks usestate add to array react how to push an object in an array usestate add array of objects to usestate react usestate add item to array add objects in setstate array react native react push and object to array usestate adding items array using usestate how to add an object array in hooks push object into array with state hooks useState array add item react usestate add to array of objects how to add an object to an array with usestate how to add an object to an array with use state append new object to array react hooks usingstate add object to a array using hooks how to add array in object using reactjs setState add objeto a array usestate react hoow to add element in array in usestate react react setstate add object to array how to add value in existing object in array in usestate how add inside object of arra y in usestate add object to array react.js react hooks push object in array useState usestate add array how to change array of object inside object in usestate how to change array of object in usestate how to push to an array in state usestate how to set useState array? add objects to an array usestate update an element from an array in usestate react add to usestate array update array state react usestate hook usestate to array usestate dynamic array useState add items to array how to push value in array of objects in usestate how to concat array using useState in react how to appendinside usestate array usestate react array add array of object usestate react how to append value in array form usestate how to push into array in useState in react js update array state in react js usestate usestate array react usestate array how to add array to object in javascript usestate how to push the object in array using usestate add item to array in useState how to push objects in an array using usestate set array hook which method is used to add new element to an array with useState update array in usestate useState for arrays react usesetstate array react usestate array how to add dummy array in useState usestate array of objects react hooks add item to state array array of object using hooks how to push value to array using usestate in react usestate add to array useState array react react hooks array example add items to a react array in hooks how to switch element in array using useState in react append new array state hooks update useState by appending to an array new array setstate hooks add number to an array usestate how to update array in usestate where to update state array in react js hooks how to use react.usestate for allray of objects how to define array in react usestate add data in usestate react hooks array push state variable using useState pushing t array using use stsate react how to add item to usestate array
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source